Asp.net mvc MVC4列表模型向控制器返回null
这是我的密码: 型号Asp.net mvc MVC4列表模型向控制器返回null,asp.net-mvc,list,asp.net-mvc-4,null,Asp.net Mvc,List,Asp.net Mvc 4,Null,这是我的密码: 型号 public class InformationsModel { public List<InformationModel> infos { get; set; } public InformationsModel() { } } public class InformationModel { public InformationModel() { } public string Name { ge
public class InformationsModel
{
public List<InformationModel> infos { get; set; }
public InformationsModel()
{
}
}
public class InformationModel
{
public InformationModel() {
}
public string Name { get; set; }
public string Value { get; set; }
public string Group { get; set; }
public string Type { get; set; }
public bool Avaiable { get; set; }
}
模板
@model InterfaceWeb.Models.Information.InformationModel
<div class="infoMain">
<div class="colunas">
@Html.TextBoxFor(x => x.Name, new { style = "width:250px;" })
</div>
<div class="colunas">
@Html.TextBoxFor(x => x.Value, new { style = "width:120px;" })
</div>
<div class="colunas">
@Html.TextBoxFor(x => x.Group, new { style = "width:120px;" })
</div>
<div class="colunas">
@Html.TextBoxFor(x => x.Type, new { style = "width:150px;" })
</div>
<div class="colunas">
@Html.CheckBoxFor(x => x.Avaiable, new { style = "width:10px; margin-left:20px;" })
</div>
</div>
当我到达控制器时,我的模型是空的,我不知道为什么
我试图在之后、之前更改模板、视图、初始化列表,但没有任何效果
谢谢您的帮助!=) 由于模型前缀的工作方式,您遇到了这个问题。具体而言,问题在于:
[HttpPost]
public ActionResult UpdateInformations(InformationsModel infos)
{
}
如果您查看一下模板生成的HTML,您会发现它大致如下所示:
<input id="infos_0__Name" name="infos[0].Name" type="text" value="" />
另外,我建议将
InformationModel
重命名为更合适的名称,如Person
、Employee
或您正在建模的任何东西。仅仅因为类名几乎完全相同,而且名称并不能真正传达它们的意图,所以执行您尝试执行的操作有点困难。您的问题可能是事物的命名:)
更改控制器:
[HttpPost] public ActionResult UpdateInformations(InformationsModel infos) { }
与:
或者在模型列表中,将名称“infos”更改为其他名称
例如:
公共类信息模型
{
公共列表信息{get;set;}
公共信息模型()
{
}
}
更改为:
公共类信息模型
{
公共列表另一个名称{get;set;}
公共信息模型()
{
}
}
您的视图正在显示您想要的内容?谢谢!我有一个引用类库的多项目解决方案。我必须创建一个允许文件上传的模型,这就是我问题的解决方案。非常感谢你。
<input id="infos_0__Name" name="infos[0].Name" type="text" value="" />
[HttpPost]
public ActionResult UpdateInformations(InformationsModel model)
{
}
[HttpPost] public ActionResult UpdateInformations(InformationsModel infos) { }
[HttpPost] public ActionResult UpdateInformations(InformationsModel model) { }
public class InformationsModel
{
public List<InformationModel> infos { get; set; }
public InformationsModel()
{
}
}
public class InformationsModel
{
public List<InformationModel> anothername { get; set; }
public InformationsModel()
{
}
}