5
我试图让自定义模型绑定工作,但由于某些原因未设置值。代码似乎ligit比较它的工作代码,但它仍然没有绑定。我想这是我失踪的一些微不足道的事情。MVC模型绑定保持值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();
}
}
视图形式如下:
@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>
}
而且,所述控制器:
[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);
}
群集上请求
public partial class Cluster
{
public Cluster()
{
this.Team = new HashSet<Team>();
}
public long Id { get; set; }
public System.DateTime Created { get; set; }
public System.DateTime Modified { get; set; }
public bool Active { get; set; }
public long FoundationId { get; set; }
public string Name { get; set; }
public virtual Foundation Foundation { get; set; }
public virtual ICollection<Team> Team { get; set; }
}
我们能看到你的集群类? Item.Name绝对是公共财产而不是公共领域吗?这是一段时间,但IIRC它只会绑定到属性。 – Xefan
我已将它添加到问题 –
您是否检查过'UpdateModel'中是否有更新'model.Item'的内容? –