我有一個 MVC 表單,我將這些資料從 JS 發布到這個控制器。我無法將此資料系結到模型。當前模型正在尋找隱藏欄位為 QuestionType[int] 但我不確定如何做螞蟻幫助會很棒
看法
@model List<MeetingPolling>
int idx = 0;
foreach (var item in Model)
{
<div class="form-group row">
<div class="col-md-12">
@Html.HiddenFor(m => item.QuestionType[idx])
@Html.HiddenFor(x => item.QuestionType, new { @id = string.Format("hfQuestionType{0}", item.labelControl), @name = string.Format("hfQuestionType{0}", item.labelControl), Value = item.QuestionType })
</div>
</div>
idx ;
}
當前序列化資料
item.QuestionType=LongAnswerText&1=sssssssssssssssssssssss&item.QuestionType=MultipleChoice&16=75
什么模型正在尋找能夠系結
QuestionType%5B0%5D=LongAnswerText&textboxfor_1=456456456456456456&QuestionType%5B1%5D=MultipleChoice&radioList_16=75
模型
public class PollingResponseFormViewModel
{
public List<string> QuestionType { get; set; }
}
uj5u.com熱心網友回復:
您不能將單個Html.HiddenFor()
欄位用于 a List<String>
- 您需要以這種方式呈現這些隱藏輸入:
@for( Int32 i = 0; i < this.Model.QuestionType.Count; i ) {
@Html.HiddenFor( m => m.QuestionType[i] )
}
您必須使用for
,而不是foreach( var foo in this.Model.QuestionType )
因為Expression<>
傳入HiddenFor
的需要屬性的int
索引器List<T>
。
此外,更改您的視圖模型,使其QuestionType
成為預初始化的get
-only 屬性,否則null
默認情況下它將與所有內容混為一談:
public class PollingResponseFormViewModel
{
public List<string> QuestionType { get; } = new List<String>();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/525632.html