【问题标题】:Cannot get correct syntax for @Html.ListBoxFor()无法获得 @Html.ListBoxFor() 的正确语法
【发布时间】:2016-09-05 02:05:27
【问题描述】:

我有一系列动态构建的@Html 组件,包括ListBoxFor()。与其他人一起,我给了他们一个 ID,然后我用它来填充一个名为 inputvalues 的模型值,它在每个组件发生变化时保存它的值。这很好用,但我必须将原始的 DropDownListFor() 更改为 ListBoxFor(),但尽管新语法有效,但我无法像以前那样为其分配 ID 值而不会出现语法错误。代码看起来像这样..

@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>   

虽然原始 DropDownlistFor() 的语法有效并且更新了输入值,但该组件不起作用。将其更改为 ListBoxFor() 后,该组件可以工作,但我似乎无法在不出错的情况下分配 ID 'personField_'。 任何帮助将不胜感激。

【问题讨论】:

  • 是什么让您认为需要更改该方法创建的默认id 属性?
  • 因为现在 DropDownListFor() 被注释掉并且 ListBoxFor() 没有 ID,当一个项目被选中时,脚本永远不会运行。尽管它使用 DropDownListFor() 做了,但组件的选择是错误的。如何将相同的 ID 分配给 ListBoxFor(0 以便脚本运行?
  • 您的ListBoxFor() 方法正在生成&lt;select id="ListOptions" ... &gt;,所以只需使用$("#ListOptions").change(function () {
  • 成功了,谢谢

标签: javascript asp.net-mvc razor html.listboxfor


【解决方案1】:

我看不到您尝试在 ListBoxFor 助手中分配 ID。

应该是这样的:

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

你的模型的SelectedListOptionIDs字段应该是你的ID类型的IListIEnumerable,或Array(可能是IList&lt;int&gt;)。然后它将在 View 上正常工作并在表单 POST 上正确绑定。

【讨论】:

  • 感谢您的回复,它可以正常工作,因为它与文件类型作为我的 IEnumerable 类列表一样,但是我怎样才能像使用原始 DropDownListFor() 一样为 ListBoxFor() 提供一个 ID?跨度>
  • @user616076 我想有很简单的方法可以做到这一点。最好用单个值初始化您的 IEnumerable,这样它就可以正常工作了
【解决方案2】:

参见上面 Stephen Meucke 的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多