我需要在表单内张贴对象列表,它包含另一个列表,但我得到了这个错误:
索引超出范围。必须为非负数且小于集合的大小
我的模型:
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]开始。调查选择[a]。有什么建议吗?
您的代码中有一个bug
@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++)
在你的代码中有一些错误,变量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