44 6f 6f 20 44 6f 6f
44 6f 6f 20 44 6f 6f

Reputation: 63

Appending list items to MVC DropDownList with jQuery

I am trying to dynamically append list items to the MVC DropDownListFor with jQuery. Unfortunately nothing happens when I run my code. I have used console.log to double check the html that's being generated, and it is. Any help would be appreciated!

Here is the jQuery

function formHelpers() {
jQuery.fn.addDropDownValues = function (options) {

    // Grab the class selector given to the function
    var itemClass = $(this);

    // Iterate through each element and value
    $.each(options, function (val, text) {
        itemClass.append("<option></option>").val(val).html(text);
    });
  };

}; 

$(document).ready(function () {
        formHelpers();
        $(".clrDropDown").addDropDownValues({Val1:"Yes", Val2:"No"});
    });

This is the cshtml

<div style="float:left;margin:0 10px 0 0;">
    <table class="ntiTable">
        <tbody>

           <td nowrap class="ntiTableFirstColumn" title="This is the Crl Collaboration  field."><div class="editor-label">@Html.LabelFor(m => m.myProject.CrlCollaboration)</div></td>
           <td><div class="editor-field">@Html.DropDownListFor(model => model.myProject.CrlCollaboration, new SelectList(""), new { @class = "crlDropDown"})@Html.ValidationMessageFor(model => model.myProject.CrlCollaboration)</div></td>

        </tbody>
    </table>
</div>

The generated CSHTML

 <tr>
            <td class="ntiTableFirstColumn" title="This is the Crl Collaboration field." nowrap="nowrap"><div class="editor-label"><label for="myProject_CrlCollaboration">Crl Collaboration</label></div></td>
            <td><div class="editor-field"><select class="crlDropDown" data-val="true" data-val-length="The field Crl Collaboration must be a string with a maximum length of 10." data-val-length-max="10" id="myProject_CrlCollaboration" name="myProject.CrlCollaboration"></select><span class="field-validation-valid" data-valmsg-for="myProject.CrlCollaboration" data-valmsg-replace="true"></span></div></td>
 </tr>

Upvotes: 0

Views: 4727

Answers (2)

Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 67171

http://jsfiddle.net/enbF7/4/ - Updated jsFiddle with your generated cshtml, it works now. The class 'clrDropDown' was spelt wrong, it's spelt 'crlDropDown' in the generated HTML.

The problem is within your $.each() function...

Looks like simply how you formatted the value & text into the <option> Also I think your itemClass.append(<option>).val(val)) <-- you were here trying to give the itemClass a value, hence why it was breaking

    $.each(options, function(value, text) {   
        itemClass
            .append($('<option>', { value : value})
            .text(text)); 
    });

Check out the jsfiddle to see it in action, hope that helps!

Upvotes: 3

DrColossos
DrColossos

Reputation: 12998

While the post by mcpDESIGNS works (+1), I think he doesn't want an already available select.

I updated his fiddle with a new revision that does the same without adding an already existing list with prepopulated values.

http://jsfiddle.net/enbF7/2/

Upvotes: 0

Related Questions