我需要在表單內發布物件串列,它包含另一個串列,但出現此錯誤:
指數超出范圍。必須是非負的并且小于集合的大小
我的模型:
public class SurveyQuestionDto
{
public SurveyQuestionDto()
{
SurveyChoices = new List<SurveyChoiceDto>();
}
public int SurveyQuesId { get; set; }
public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; }
[Required(ErrorMessage = "Required")]
public short ChoicesType { get; set; }
[StringLength(500, ErrorMessage = "MaxLength")]
public string TextAnswer { get; set; }
public virtual List<SurveyChoiceDto> SurveyChoices { get; set; }
}
public class SurveyChoiceDto
{
[Required(ErrorMessage = "Required")]
public int? LanguageId { get; set; }
[Required(ErrorMessage = "Required"),
StringLength(200, ErrorMessage = "MaxLength")]
public string Title { get; set; }
public long ChoiceId { get; set; }
public int SurveyQuesId { get; set; }
public long Count { get; set; }
public bool MarkedToDelete { get; set; }
public bool IsSelected { get; set; }
}
我的觀點 :
@model List<SurveyQuestionDto>
@if (Model != null && Model.Count() > 0)
{
<form action="@Url.Action("Answer", "Survey", new { typeId })" method="post" class="form form-horizontal" enctype="multipart/form-data">
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@foreach (var question in Model)
{
int i = 0;
@Html.HiddenFor(m => m[i].SurveyQuesId, new { Value = question.SurveyQuesId })
<div style="background:#cccccc; padding:20px; margin:20px;">
<h2>@question.Title</h2>
@if (question.ChoicesType == (int)ChoicesTypeEnum.Text)
{
@Html.TextAreaFor(m => m[i].TextAnswer, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", _loc["Required"] } })
}
else
{
<ul style="list-style-type:none;">
@foreach (var choice in question.SurveyChoices)
{
int a = 0;
<li>
@Html.HiddenFor(m => m[i].SurveyChoices[a].ChoiceId, new { Value = choice.ChoiceId })
@if (question.ChoicesType == (int)ChoicesTypeEnum.SingleChoice)
{
@Html.RadioButtonFor(m => m[i].SurveyChoices[a].IsSelected, null, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", _loc["Required"] } })
}
else
{
@Html.CheckBoxFor(m => m[i].SurveyChoices[a].IsSelected, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", _loc["Required"] } })
}
<label>@choice.Title</label>
</li>
a ;
}
</ul>
}
</div>
i ;
}
<button type="submit">Submit now</button>
</form>
}
模型物件已經有了它的值:
public async Task<IActionResult> Index(int id)
{
return View(await _rep.GetSurveyQuestionsWithChoices(id));
}
問題始于這行代碼m[i].SurveyChoices[a].ChoiceId 有什么建議嗎?
uj5u.com熱心網友回復:
你的代碼有錯誤
@foreach (var question in Model)
{
int i = 0;
@Html.HiddenFor(m => m[i].SurveyQuesId, new { Value = question.SurveyQuesId })
對于每個專案,您將始終具有 i=0,因為它會重復分配零。
使用 for 回圈要好得多
@for(var i=0; i < Model.Count; i )
{
@Html.HiddenFor(m => m[i].SurveyQuesId, new { Value = @Model[i].SurveyQuesId })
....and so on
代碼中的第二個 foreach 回圈也是如此。應該是
@for (var a=0; a < @Model[i].SurveyChoices.Count; a )
uj5u.com熱心網友回復:
你的代碼中有一些錯誤,變數i,a永遠不會改變。你應該在你的foreach回圈中手動處理這個,如下所示:
@foreach (var question in Model)
{
int i = 0;
// rest of your code
i ;
}
或者您可以使用for回圈而不是foreach:
for(int index=0;index<Model.Count;index ){...}
除了論文之外,您可以在代碼中省略這個 if 陳述句,因為當沒有專案時,for回圈不會運行:
@if (Model != null && Model.Count() > 0)
或者你可以Any()像下面這樣使用:
@if (Model.Any()){...}
請記住,如果您的存盤中沒有資料,您應該回傳空串列而不是 null
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/386285.html
標籤:C# asp.net-mvc asp.net核心 .net核心
