简体   繁体   中英

Cannot get correct syntax for @Html.ListBoxFor()

I've a series of @Html components which are built dynamically including ListBoxFor(). With the others I've given them an ID which I then use to populate a model value called inputvalues, which holds the values of each component whenever it changes. This works well but I had to change the original DropDownListFor() for ListBoxFor() but although the new syntax works, I cannot assign it an ID value as I did before without getting a syntax error. The code looks like this..

@if (Model != null)
{  
    @Styles.Render(BundleConfig.Styles_MultiSelect)

    IEnumerable<SelectListItem> filetypes = from filetype in Model.ListOptions
    select new SelectListItem
    {
        Value = filetype.ID.ToString(),
        Text = filetype.Name,
        Selected = Model.SelectedListOptionID == null ? false : Model.SelectedListOptionID > 0
    };

    <div class="editor-section">
        <div class="label">
            @Html.DisplayEditLabel(Model.Label, Model.Required.Value)
        </div>
        <div class="field large-text-field">
            @*Original drop down replaced by ListBoxFor() but with ID
            @Html.DropDownListFor(m => m.SelectedListOptionID, new SelectList(Model.ListOptions, "ID", "Name", Model.SelectedListOptionID).OrderBy(l => l.Value), new Dictionary<string, object>{
                 {"id", "personField_" + Model.ID}})*@

            @Html.ListBoxFor(m => m.ListOptions, filetypes, new { @class = "multiselectFileTypes" })
        </div>
    </div>
}    
@Scripts.Render(BundleConfig.Scripts_MultiSelect)
<script>
    $("#personField_" + "@Model.ID").change(function () {
        cnt++;

        var uploadValue = JSON.stringify({
            "id": "@Model.ID",
            "order": cnt,
            "required": "@Model.Required",
            "libraryUploadConfigType": 3,
            "customFieldTypeID": 5,
            "selectedListOptionID": $(this).val()
        });

        inputValues = inputValues + uploadValue;
    });


    $(".multiselectFileTypes").multiselect({
        noneSelectedText: 'All Options',
        minWidth: 230,
        selectedList: 6
    });
</script>   

Although the syntax for the original DropDownlistFor() worked and updated inputvalues the component didn't work. Having changed it to ListBoxFor() the component works but I can't seem to assign the ID 'personField_' without getting an error. Any help would be appreciated.

I can't see that you try to assign ID in your ListBoxFor helper.

It should be like this:

 @Html.ListBoxFor(m => m.SelectedListOptionIDs, filetypes, new { @class = "multiselectFileTypes" })

And SelectedListOptionIDs field of your model should be IList or IEnumerable , or Array of your ID type (probably IList<int> ). Then it will work fine on View and bind correct on form POST .

请参阅上述斯蒂芬·穆克的答复。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM