我有一個 MVC 表單,我將這些資料從 JS 發布到這個控制器。我無法將此資料系結到模型。當前模型正在尋找隱藏欄位為 QuestionType[int] 但我不確定該怎么做,螞蟻幫助會很棒
網頁格式
<input data-val="true" data-val-required="The Char field is required." id="item_QuestionType_0_" name="item.QuestionType[0]" type="hidden" value="L">
看法
@model List<MeetingPolling>
int idx = 0;
foreach (var item in Model)
{
<div class="form-group row">
<div class="col-md-12">
@Html.HiddenFor(m => Model[idx].QuestionType)
@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/houduan/535753.html
上一篇:每次我重新啟動服務器時,我的Laravel鏈接都會中斷
下一篇:值為=1時重繪頁面
