MVC2 ModelBinder 未绑定到“id”;模型的属性
我有一个名为 Project 的模型,它具有以下属性(为简洁起见进行了简化)。
[DataContract(IsReference = true)]
public class Project
{
[Key]
[DataMember]
public int id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public DateTime CreatedDate { get; set; }
}
该模型也是使用实体框架 4 查询数据存储的 WCF 服务的数据协定。该模型的代码是从模板生成的,该模板根据实体框架模型自动生成 CRUD 服务层。
现在,我的 MVC2 应用程序有一个视图,其中包含用于编辑字段的表单。控制器的“编辑”操作接受整个模型作为 POST 时的参数。
[HttpPost]
public ActionResult Edit(Project project)
{
var context = new ServiceContext();
try
{
if (ModelState.IsValid)
{
project = context.UpdateProject(project);
return RedirectToAction("Index");
}
}
catch
{
ModelState.AddModelError("", "Could not save project");
}
return View(project);
}
现在,我的问题是,当表单发布到控制器时,项目模型的所有字段都已正确填充,“id”属性除外,默认为 0。
我已经做了一些挖掘并恳求谷歌叔叔提供答案,但我能得到的最接近的解决方案是将以下内容添加到模型的类中,
[Bind(Include="id")]
这工作正常,但仅填充“id”属性,这意味着我必须明确指定每个属性包含在模型绑定中。显然,这可能会变得很糟糕,特别是因为模型本身比我上面显示的属性有更多的属性。
还有其他方法可以让这个工作吗?
直觉是 [Key] 属性与此有关,但我无法弄清楚任何事情。
该表单具有“id”属性的隐藏输入。
<%: Html.HiddenFor(model => model.id)%>
I have a model called Project, that has the following properties (simplified for brevity's sake).
[DataContract(IsReference = true)]
public class Project
{
[Key]
[DataMember]
public int id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public DateTime CreatedDate { get; set; }
}
The model is also a datacontract for a WCF service that uses Entity Framework 4 to query a datastore. The code for the model is generated from a template that automatically generates a CRUD service layer against the Entity Framework Model.
Now, my MVC2 application has a view containing a form to edit the fields. The controllers Edit action accepts the entire model as an argument upon POST.
[HttpPost]
public ActionResult Edit(Project project)
{
var context = new ServiceContext();
try
{
if (ModelState.IsValid)
{
project = context.UpdateProject(project);
return RedirectToAction("Index");
}
}
catch
{
ModelState.AddModelError("", "Could not save project");
}
return View(project);
}
Now, my problem is that when the form is posted to the controller, the Project model has all its fields correctly populated except for the 'id' property, which defaults to 0.
I've done some digging and pleaded with Uncle Google for answers, but the closest fix I could get was to add the following to the model's class,
[Bind(Include="id")]
which works fine, but ONLY populates the 'id' property, meaning that I would have to explicitly specify each property to be included in the model binding. Obviously, this can get nasty, especially since the model itself has many more properties than the one's I've shown above.
Is there any other way to get this working?
Gut feel is that the [Key] attribute has something to do with it, but I haven't been able to figure anything out.
The form has a hidden input for the 'id' property.
<%: Html.HiddenFor(model => model.id)%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我已经找到了解决方案。
我的模型还有一些复杂的属性,它们映射到实体框架模型中的相关表。现在,由于我使用自我跟踪实体 T4 模板来生成服务层,因此在将值映射到实体时它具有一些附加逻辑。在本例中,复杂属性之一是名为 Status 的类,如下所示:
ProjectId 是我的数据存储中的 Project 表的外键。我注意到在模型绑定期间 ProjectId 字段也设置为 0。
当我将以下内容添加到视图中时,
两个字段都将正确的值发布到控制器。
问题已解决:)
感谢 swapneel 您对此事的想法。
I think I have found the solution.
My model also has some complex properties which map to related tables in my entity framework model. Now, since I'm using the Self-Tracking Entities T4 templates to generate my service layer, it has some additional logic when it comes to mapping values to entities. In this case, one of the complex properties is a class called Status which looks like this:
The ProjectId is a foreign key to the Project table in my datastore. I noticed that the ProjectId field was also set to 0 during model binding.
When I added the following to my view
both fields had the correct value posted to the controller.
Problem solved :)
Thanks swapneel for your thoughts on the matter.
尝试为 ID 添加额外的隐藏字段,例如 <%: Html.HiddenFor(model => model.Id) %>
Try Adding additional hidden field for ID like <%: Html.HiddenFor(model => model.Id) %>