我试图让自定义模型绑定工作,但由于某种原因,这些值没有设置。将代码与工作代码进行比较时,该代码似乎是合法的,但它仍然没有绑定。我想这是我错过的一些微不足道的事情。
定制型号:
//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; }
}