C# MVC模型绑定保持值为空
我试图让自定义模型绑定工作,但由于某些原因,这些值没有设置。当将代码与工作代码进行比较时,该代码看起来很简单,但仍然没有绑定。我想这是我错过的一件小事 自定义模型:C# MVC模型绑定保持值为空,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我试图让自定义模型绑定工作,但由于某些原因,这些值没有设置。当将代码与工作代码进行比较时,该代码看起来很简单,但仍然没有绑定。我想这是我错过的一件小事 自定义模型: //Cluster is from Entity Framework //BaseViewModelAdmin defines: public List<KeyValuePair<string, string>> MenuItems; public IPrincipal CurrentUser = null
//Cluster is from Entity Framework
//BaseViewModelAdmin defines:
public List<KeyValuePair<string, string>> MenuItems;
public IPrincipal CurrentUser = null;
public Foundation Foundation; //also from Entity Framework
public class AdminClusterCreateModel : BaseViewModelAdmin
{
public Cluster Item;
public AdminClusterCreateModel()
{
Item = new Cluster();
}
}
//集群来自实体框架
//BaseViewModelAdmin定义:
公共列表菜单项;
公共IPrincipal CurrentUser=null;
公共基金会;也来自实体框架
公共类AdminClusterCreateModel:BaseViewModelAdmin
{
公共分组项目;
公共AdminClusterCreateModel()
{
Item=新集群();
}
}
视图窗体如下所示:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Cluster</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Item.Active)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Item.Active)
@Html.ValidationMessageFor(model => model.Item.Active)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Item.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Item.Name)
@Html.ValidationMessageFor(model => model.Item.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
簇
@LabelFor(model=>model.Item.Active)
@EditorFor(model=>model.Item.Active)
@Html.ValidationMessageFor(model=>model.Item.Active)
@LabelFor(model=>model.Item.Name)
@EditorFor(model=>model.Item.Name)
@Html.ValidationMessageFor(model=>model.Item.Name)
}
以及控制器:
[HttpPost]
public ActionResult Create(AdminClusterCreateModel model, FormCollection form)
{
if(ModelState.IsValid) //true
{
var test = form["Item.Name"]; //Value is correct from form (EG: Test)
UpdateModel(model); //no error
}
//At this point model.Item.Name = null <--- WHY?
return View(model);
}
[HttpPost]
公共操作结果创建(AdminClusterCreateModel模型,FormCollection表单)
{
if(ModelState.IsValid)//true
{
var test=form[“Item.Name”];//表单中的值是正确的(例如:test)
UpdateModel(模型);//无错误
}
//此时,model.Item.Name=nullDefaultModelBinder显式处理“属性”,而不是“字段”
将AdminClusterCreateModel
中的public Cluster Item
更改为public Cluster Item{get;set;}
,应该可以做到这一点
public class AdminClusterCreateModel : BaseViewModelAdmin
{
public Cluster Item {get; set;}
public AdminClusterCreateModel()
{
Item = new Cluster();
}
}
关于我们可以看到你的集群类吗?是Item。命名肯定是一个公共属性而不是公共字段?已经有一段时间了,但IIRC它只会绑定到属性。我已经将它添加到问题中。你是否检查了UpdateModel
中是否有更新model.Item
的内容?默认情况下@HugoDelsing'DefaultModelBinder'开始发挥作用,并从“FormCollection”构造“object in action”方法参数。有一种方法可以通过“Custom Model Binder”(相关)更改特定类型的绑定工作。我问这个问题是因为我可能误读了“我正在尝试让自定义模型绑定工作”。现在我认为没有“自定义绑定器”,并且“DefaultModelBinder”仍在发挥作用。在这种情况下,您可能需要将“public Cluster Item;”更改为“public Cluster Item{get;set;};”,以使其工作。谢谢!的确public Cluster Item{get;set;}
足以让它工作。我对误导性信息表示歉意。我绑定到一个自定义模型,因此我使用了该术语。如果您将其作为答案发布,我将接受它。
public class AdminClusterCreateModel : BaseViewModelAdmin
{
public Cluster Item {get; set;}
public AdminClusterCreateModel()
{
Item = new Cluster();
}
}