[英]View model with complex type is null when passed to controller
I am trying to pass a view model with complex types to my controller. 我试图将复杂类型的视图模型传递给我的控制器。 I have researched everything I can top to bottom over this subject and I am still confused.
我已经研究过所有关于这个主题的自上而下的内容,我仍然感到困惑。
The Problem: 问题:
When I click my submit button, the view model is passed in but the List of MacroInfo property is null. 单击我的提交按钮时,将传入视图模型,但是MacroInfo列表属性为null。
UpdateIndexViewModel UpdateIndexViewModel
public class UpdateIndexViewModel
{
//This view model will become larger later
public List<MacroInfo> MacrosToUpdate { get; set; }
}
MacroInfo MacroInfo
public class MacroInfo
{
public bool IsSelected { get; set; }
public string FullPath { get; set; }
public string Id { get; set; }
public DateTime CreatedAt { get; set; }
}
Controller Action 控制器动作
[HttpPost]
public ActionResult Submit(UpdateIndexViewModel updateIndexViewModel)
{
//updateIndexViewModel.MacrosToUpdate is null ??
}
Index View 索引视图
@model EplanInterface.Core.ViewModels.UpdateIndexViewModel
@using (Html.BeginForm("Submit", "Update", FormMethod.Post))
{
<table style="width:100%" , class="table-bordered">
<thead>
<tr>
<th>#</th>
<th>Macro Path</th>
<th>Created At</th>
<th>Update</th>
</tr>
</thead>
@for (int i = 1; i < Model.MacrosToUpdate.Count; i++)
{
<tr>
<td>@Html.TextBoxFor(m =>Model.MacrosToUpdate[i].FullPath)</td>
<td>@Html.TextBoxFor(m => Model.MacrosToUpdate[i].CreatedAt)</td>
<td>@Html.CheckBoxFor(b => Model.MacrosToUpdate[i].IsSelected)</td>
</tr>
}
</table>
<input type="submit" class="btn btn-primary" value="Submit"/>
}
What I have tried 我试过了什么
I tried changing the controller actions property being passed in to List<MacroInfo> macrosToUpdate
, but when doing this the property is still null. 我尝试将传入的控制器动作属性更改为
List<MacroInfo> macrosToUpdate
,但在执行此操作时,该属性仍为null。
Chrome network inspection Chrome网络检查
Final Remarks 最后的评论
I am not sure if I need to be using an AJAX post to do this, or if my variable names just are not formatted correctly. 我不确定是否需要使用AJAX帖子来执行此操作,或者我的变量名称是否格式不正确。 I am pretty sure it is a binding issue I am not understanding.
我很确定这是一个具有约束力的问题,我不理解。
If anyone could point me in the right direction I would really appreciate it. 如果有人能指出我正确的方向,我会非常感激。
This part of your template is bit wrong. 这部分模板有点不对劲。
@for (int i = 1; i < Model.MacrosToUpdate.Count; i++)
{
<tr>
<td>@Html.TextBoxFor(m =>Model.MacrosToUpdate[i].FullPath)</td>
<td>@Html.TextBoxFor(m => Model.MacrosToUpdate[i].CreatedAt)</td>
<td>@Html.CheckBoxFor(b => Model.MacrosToUpdate[i].IsSelected)</td>
</tr>
}
Please change with following and try again. 请更改以下内容,然后重试。
@for (int i = 0; i < Model.MacrosToUpdate.Count;
{
<tr>
<td>@i</td>
<td>@Html.TextBoxFor(m => m.MacrosToUpdate[i].FullPath)</td>
<td>@Html.TextBoxFor(m => m.MacrosToUpdate[i].CreatedAt)</td>
<td>@Html.CheckBoxFor(b => b.MacrosToUpdate[i].IsSelected)</td>
</tr>
}
First you were starting the loop with 1, which was the root cause. 首先,你用1开始循环,这是根本原因。 Model binder wasn't able to bind the list properly due to missing zeroth index.
由于缺少第0个索引,模型绑定器无法正确绑定列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.