[英]Cannot get correct syntax for @Html.ListBoxFor()
我有一系列@Html組件,這些組件是動態構建的,包括ListBoxFor()。 與其他人一起,我給了他們一個ID,然后使用該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
字段應該是IList
或IEnumerable
,或者是ID類型的Array
(可能是IList<int>
)。 然后它將在View上正常工作,並在POST表單上正確綁定。
請參閱上述斯蒂芬·穆克的答復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.