无法获得 @Html.ListBoxFor() 的正确语法
Cannot get correct syntax for @Html.ListBoxFor()
我有一系列 @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_'。
任何帮助将不胜感激。
我看不到你试图在你的 ListBoxFor
助手中分配 ID。
应该是这样的:
@Html.ListBoxFor(m => m.SelectedListOptionIDs, filetypes, new { @class = "multiselectFileTypes" })
而你模型的SelectedListOptionIDs
字段应该是你ID类型的IList
或IEnumerable
,或Array
(可能是IList<int>
)。然后它将在 View 上正常工作,并在表单 POST.
上正确绑定
参见上面 Stephen Meucke 的回复。
我有一系列 @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_'。 任何帮助将不胜感激。
我看不到你试图在你的 ListBoxFor
助手中分配 ID。
应该是这样的:
@Html.ListBoxFor(m => m.SelectedListOptionIDs, filetypes, new { @class = "multiselectFileTypes" })
而你模型的SelectedListOptionIDs
字段应该是你ID类型的IList
或IEnumerable
,或Array
(可能是IList<int>
)。然后它将在 View 上正常工作,并在表单 POST.
参见上面 Stephen Meucke 的回复。