Search code examples
asp.net-mvc-3razorrazor-declarative-helpers

Creating Razor radio buttons with AJAX


I currently have a Razor view the makes an AJAX (getJSON) call. The Ajax call requests a list of schools that work with us and "append" the data to a @Html.DropDownListFor called schoolID.

$.getJSON('/Account/ajaxGetSchools', function (result) {

    SchoolID.empty();
    $(result).each(function () {
        SchoolID.append(
            $('<option/>', {
                value: this.Id
            }).html(this.Nome)
        );

    });

My new requirement is to replace the dropdownlist with dynamically created radio buttons.

This would be easy if i simply used html controls. I would create a group of input-radios. But this seems sloppy from a razor-model-binding paradigm. But I don't think I can make @Html.RadioButton calls within JS.

Any suggestions?


Solution

  • One option would be to have the AJAX call return a partial view that you build using the Html.RadioButton Razor syntax.

    When the main form submits, you can then just parse the input for the radio button values you inserted dynamically.