C# ASP.NET 使用 Html.RenderPartial 父子视图之间的 MVC 错误共享模型
C# ASP.NET MVC error sharing model between parent and child view using Html.RenderPartial
调用 @Html.RenderPartial("_ChildPartialView")
时出现以下错误:
System.Collections.Generic.ICollection' has no applicable method named 'ElementAt' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax
_Testpaper.cshtml父视图:
for (i = 0; i < Model.Questions.Count;i++)
{
ViewBag.QuestionNumber = i;
Html.RenderPartial("_QuestionDetail"); //Line causing error
}
_QuestionDetail.cshtml 子视图:
@model StandardVBA.ViewModels.AssessmentModel
<tr style="padding:4px 0px; background-color:lightskyblue; font-weight:bold;font-family:Cambria;">
<td style="text-align:left;">
Q @(ViewBag.QuestionNumber + 1)   @Model.Questions.ElementAt(ViewBag.QuestionNumber).Question
</td>
<td style="text-align:center">
( @Model.Questions.ElementAt(ViewBag.QuestionNumber).Marks )
</td>
</tr>
<tr>
<td class="questions">
<ol type="A">
@for (int j = 0; j < Model.Questions.ElementAt(ViewBag.QuestionNumber).QuestionDetails.Count; j++)
{
<li>
<div style="display: inline-block; vertical-align: top;">
@Html.CheckBoxFor(m => m.Questions.ElementAt(ViewBag.QuestionNumber).QuestionDetails.ElementAt(j).IsSelected)
</div>
@Html.DisplayFor(m => m.Questions.ElementAt(ViewBag.QuestionNumber).QuestionDetails.ElementAt(j).Choice)
@Html.HiddenFor(m => m.Questions.ElementAt(ViewBag.QuestionNumber).QuestionDetails.ElementAt(j).IsCorrect)
</li>
}
</ol>
</td>
</tr>
我也想知道:当子视图在RenderPartial
调用中共享相同模型时,为什么必须在子视图中指定@Model
?
首先,您没有将模型传递给您的子视图,而是在子视图中使用@model,因此通过将模型传递给您的子视图来修复它
for (i = 0; i < Model.Questions.Count;i++)
{
ViewBag.QuestionNumber = i;
Html.RenderPartial("_QuestionDetail", Model); //Line causing error
}
其次,您在作为子视图的详细视图中使用了@Html.CheckBoxFor(m => m.Questions.......),因此您需要声明@model。 ..... 在您的视图中使用模型。
希望这能奏效!
您需要像这样将模型传递给子局部视图:
for (i = 0; i < Model.Questions.Count;i++)
{
ViewBag.QuestionNumber = i;
Html.RenderPartial("_QuestionDetail", Model.Questions[i]); //Line causing error
}
确保 Model.Questions[i] 的类型与子局部视图中的模型声明相匹配 "@model StandardVBA.ViewModels.AssessmentModel" 否则会出现运行时错误。
希望对您有所帮助。
调用 @Html.RenderPartial("_ChildPartialView")
时出现以下错误:
System.Collections.Generic.ICollection' has no applicable method named 'ElementAt' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax
_Testpaper.cshtml父视图:
for (i = 0; i < Model.Questions.Count;i++)
{
ViewBag.QuestionNumber = i;
Html.RenderPartial("_QuestionDetail"); //Line causing error
}
_QuestionDetail.cshtml 子视图:
@model StandardVBA.ViewModels.AssessmentModel
<tr style="padding:4px 0px; background-color:lightskyblue; font-weight:bold;font-family:Cambria;">
<td style="text-align:left;">
Q @(ViewBag.QuestionNumber + 1)   @Model.Questions.ElementAt(ViewBag.QuestionNumber).Question
</td>
<td style="text-align:center">
( @Model.Questions.ElementAt(ViewBag.QuestionNumber).Marks )
</td>
</tr>
<tr>
<td class="questions">
<ol type="A">
@for (int j = 0; j < Model.Questions.ElementAt(ViewBag.QuestionNumber).QuestionDetails.Count; j++)
{
<li>
<div style="display: inline-block; vertical-align: top;">
@Html.CheckBoxFor(m => m.Questions.ElementAt(ViewBag.QuestionNumber).QuestionDetails.ElementAt(j).IsSelected)
</div>
@Html.DisplayFor(m => m.Questions.ElementAt(ViewBag.QuestionNumber).QuestionDetails.ElementAt(j).Choice)
@Html.HiddenFor(m => m.Questions.ElementAt(ViewBag.QuestionNumber).QuestionDetails.ElementAt(j).IsCorrect)
</li>
}
</ol>
</td>
</tr>
我也想知道:当子视图在RenderPartial
调用中共享相同模型时,为什么必须在子视图中指定@Model
?
首先,您没有将模型传递给您的子视图,而是在子视图中使用@model,因此通过将模型传递给您的子视图来修复它
for (i = 0; i < Model.Questions.Count;i++)
{
ViewBag.QuestionNumber = i;
Html.RenderPartial("_QuestionDetail", Model); //Line causing error
}
其次,您在作为子视图的详细视图中使用了@Html.CheckBoxFor(m => m.Questions.......),因此您需要声明@model。 ..... 在您的视图中使用模型。
希望这能奏效!
您需要像这样将模型传递给子局部视图:
for (i = 0; i < Model.Questions.Count;i++)
{
ViewBag.QuestionNumber = i;
Html.RenderPartial("_QuestionDetail", Model.Questions[i]); //Line causing error
}
确保 Model.Questions[i] 的类型与子局部视图中的模型声明相匹配 "@model StandardVBA.ViewModels.AssessmentModel" 否则会出现运行时错误。
希望对您有所帮助。