发布包含其他模型的对象
Posting objects containing other models
我一直在寻找一种方法来 post 一个包含其他模型的模型的所有信息,我相信我可以将对象发送到我的视图并离开我的 50 个示例我看过并且可以很好地渲染一切。
这是我正在谈论的命名设备模型。
public int id { get; set; }
public String name { get; set; }
public ManufacturerItem manufacturerItem { get; set; }
public EquipmentType equipmentType { get; set; }
public SupportItem supportItem{ get; set; }
public Placement placement{ get; set; }
public Boolean status { get; set; }
public DateTime endOfLife{ get; set; }
public String notes{ get; set; }
public Purchase purchase{ get; set; }
public Boolean mes{ get; set; }
public DateTime reviewedDate{ get; set; }
基于我读过的大量示例,我知道我可以像这样渲染它们:
@Html.EditorFor(model => model.name)
@Html.EditorFor(model => model.manufacturerItem.model.name)
在其他研究中,我确实偶然发现了 building forms for deep View Model graphs in ASP.NET MVC,我可能会考虑使用它,但那是 post 在 MVC 2 天后恢复的。我正在使用 MVC 5。所以我不知道它与今天的关系如何。
假设我有另一个名为 Book 的模型,其中包含 {id, Title, Author},您可以编辑书名和作者。现在在这个模型中,在编辑时,我的控制器可能是这样的:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,Title,Author)"] Book book)
{ ... -insert code- ...}
离开这个想法,我的设备模型的控制器方法签名是什么?我是否将其他对象作为它们自己的类型包括在内?
我没有使用 EF 或 linq-to-sql,因为我必须使用存储过程。所以我想将所有这些信息整齐地打包并传递到存储库,该存储库将负责参数分配和调用存储过程。
Going off of this idea, what would be my controller method signature
be for the Equipment model?
您是否尝试过使用以下签名:
[HttpPost]
public ActionResult Edit(Equipment model)
{
...
}
顺便说一句,如果您的视图不包含允许编辑 所有 Equipment
模型对象图的属性的表单,您可以考虑使用视图模型仅包含作为输入字段包含在表单中的属性。然后在服务器上,您将使用 id 从后端获取相应的 Equipment
实例,仅更新从 HTML 表单发送的属性并将结果保存回来。
例如:
[HttpPost]
public ActionResult Edit(EquipmentViewModel model)
{
Equipment equipement = backend.GetById(model.Id);
// set the properties that are coming from the UI:
equipment.name = model.Name;
equipment.supportItem = model.SupportItem;
...
// save the updated entity back
backend.Update(equipment);
}
在此示例中,EquipmentViewModel
将仅包含您在视图中具有相应输入字段且用户应该编辑的属性,而不是整个域模型对象图。
我一直在寻找一种方法来 post 一个包含其他模型的模型的所有信息,我相信我可以将对象发送到我的视图并离开我的 50 个示例我看过并且可以很好地渲染一切。
这是我正在谈论的命名设备模型。
public int id { get; set; }
public String name { get; set; }
public ManufacturerItem manufacturerItem { get; set; }
public EquipmentType equipmentType { get; set; }
public SupportItem supportItem{ get; set; }
public Placement placement{ get; set; }
public Boolean status { get; set; }
public DateTime endOfLife{ get; set; }
public String notes{ get; set; }
public Purchase purchase{ get; set; }
public Boolean mes{ get; set; }
public DateTime reviewedDate{ get; set; }
基于我读过的大量示例,我知道我可以像这样渲染它们:
@Html.EditorFor(model => model.name)
@Html.EditorFor(model => model.manufacturerItem.model.name)
在其他研究中,我确实偶然发现了 building forms for deep View Model graphs in ASP.NET MVC,我可能会考虑使用它,但那是 post 在 MVC 2 天后恢复的。我正在使用 MVC 5。所以我不知道它与今天的关系如何。
假设我有另一个名为 Book 的模型,其中包含 {id, Title, Author},您可以编辑书名和作者。现在在这个模型中,在编辑时,我的控制器可能是这样的:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,Title,Author)"] Book book)
{ ... -insert code- ...}
离开这个想法,我的设备模型的控制器方法签名是什么?我是否将其他对象作为它们自己的类型包括在内?
我没有使用 EF 或 linq-to-sql,因为我必须使用存储过程。所以我想将所有这些信息整齐地打包并传递到存储库,该存储库将负责参数分配和调用存储过程。
Going off of this idea, what would be my controller method signature be for the Equipment model?
您是否尝试过使用以下签名:
[HttpPost]
public ActionResult Edit(Equipment model)
{
...
}
顺便说一句,如果您的视图不包含允许编辑 所有 Equipment
模型对象图的属性的表单,您可以考虑使用视图模型仅包含作为输入字段包含在表单中的属性。然后在服务器上,您将使用 id 从后端获取相应的 Equipment
实例,仅更新从 HTML 表单发送的属性并将结果保存回来。
例如:
[HttpPost]
public ActionResult Edit(EquipmentViewModel model)
{
Equipment equipement = backend.GetById(model.Id);
// set the properties that are coming from the UI:
equipment.name = model.Name;
equipment.supportItem = model.SupportItem;
...
// save the updated entity back
backend.Update(equipment);
}
在此示例中,EquipmentViewModel
将仅包含您在视图中具有相应输入字段且用户应该编辑的属性,而不是整个域模型对象图。