简体   繁体   English

无法获取@ Html.ListBoxFor()的正确语法

[英]Cannot get correct syntax for @Html.ListBoxFor()

I've a series of @Html components which are built dynamically including ListBoxFor(). 我有一系列@Html组件,这些组件是动态构建的,包括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. 与其他人一起,我给了他们一个ID,然后使用该ID填充一个称为inputvalues的模型值,该模型值将在每个组件更改时保存它们的值。 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. 这很好用,但我必须将原始的DropDownListFor()更改为ListBoxFor(),但是尽管新语法可行,但我不能像以前一样为它分配ID值,而不会出现语法错误。 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. 尽管原始DropDownlistFor()的语法有效并更新了输入值,但该组件无效。 Having changed it to ListBoxFor() the component works but I can't seem to assign the ID 'personField_' without getting an error. 将其更改为ListBoxFor()后,该组件可以正常工作,但是我似乎无法在没有错误的情况下分配ID'personField_'。 Any help would be appreciated. 任何帮助,将不胜感激。

I can't see that you try to assign ID in your ListBoxFor helper. 我看不到您尝试在ListBoxFor助手中分配ID。

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> ). 模型的SelectedListOptionIDs字段应该是IListIEnumerable ,或者是ID类型的Array (可能是IList<int> )。 Then it will work fine on View and bind correct on form POST . 然后它将在View上正常工作,并在POST表单上正确绑定。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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