1

I have a Razor page that allows users to add a variable amount of attendees to a meeting object. On the page, there is an "add attendee" button that inserts the textbox onto the page and increments the subscript so that the model binder can pick it up.

$('#addInternalAttendee').on('click', function () {
    $('#internalAttendees').append(
        '<div class=\'attendee\'>' +
            '<input type=\'text\' name=\'Attendees[' + x + ']\' class=\'form-control\' />' +
        '</div>'
    );

    ++x;
});

This piece works fine. What I'm trying to do now is to add a drop down box to select the attendee's role.

    var ddb = '@Html.DropDownList("AttendeeRoleIds[*]", (SelectList)ViewBag.EventAttendeeRoleId, htmlAttributes: new { @class = "form-control", })';

The issue I'm running into is that the HTML contains double quotes which aren't properly escaped, so the function's body on the rendered page looks like this:

$('#addInternalAttendee').on('click', function () {

    var ddb = '<select class="form-control" id="AttendeeRoleIds___" name="AttendeeRoleIds[*]"><option value="1">Host/Organizer</option>
<option value="2">Note Taker</option>
<option value="3">Supervising Manager</option>
<option value="4">Attendee (Participating)</option>
<option value="5">Attendee (Non-Participating)</option>
</select>';
    ddb = ddb.replace('\"', '\'');

    $('#internalAttendees').append(
        '<div class=\'attendee\'>' +
            '<input type=\'text\' name=\'Attendees[' + x + ']\' class=\'form-control\' />' +
            ddb +
        '</div>'
    );

    ++x;
});

Is there a way to get this HTML into the jquery function's body properly escaped so it can be rendered?

1
  • 1
    I have similar case, after use @Html.Raw() , code work fine.. Try : @Html.Raw(@Html.DropDownList("AttendeeRoleIds[*]", (SelectList)ViewBag.EventAttendeeRoleId, htmlAttributes: new { @class = "form-control", })) Commented Mar 17, 2016 at 10:24

2 Answers 2

1

You could use Json.NET to encode the string:

@Html.Raw(JsonConvert.ToString(@Html.DropDownList("AttendeeRoleIds[*]", (SelectList)ViewBag.EventAttendeeRoleId, htmlAttributes: new { @class = "form-control", })))

Do not forget to add a using-directive in your view:

@using Newtonsoft.Json;

If you have not installed Json.NET yet in your project check this http://www.newtonsoft.com/json

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$('#addInternalAttendee').on('click', function () {

var ddb = '<select class="form-control" id="AttendeeRoleIds___" name="AttendeeRoleIds[*]"><option value="1">Host/Organizer</option>
<option value="2">Note Taker</option>
<option value="3">Supervising Manager</option>
<option value="4">Attendee (Participating)</option>
<option value="5">Attendee (Non-Participating)</option>
</select>';

var d = document.createElement('div');
d.innerHTML = ddb;    

$('#internalAttendees').append(
    '<div class=\'attendee\'>' +
        '<input type=\'text\' name=\'Attendees[' + x + ']\' class=\'form-control\' />' +
        d.firstChild +
    '</div>'
);

++x;
});

2 Comments

So this ALMOST worked, problem is now that when I click the button, the input box gets rendered to the screen, but the HTML for the dropdown box, just displays on the screen. It doesn't actually render the box.
Are there single quotes around the dropdown box code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.