从表单集合中提取 [] 元素 - mvc - 应该使用 icollection 但具有混合类型
看过 Phil Haacks 的书籍项目
http ://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
很有用,但我有多种数据类型。
我使用模型视图,这样我就可以混合使用对象,在本例中:订单(即 order.id、order.date 等)、Customer、SoilSamplingOrder 和 SoilSamplingSubJobs 列表,如下所示 [0].id、[ 0].field、[1].id、[1].field 等 也许我应该使用 ICollection 而不是 List?我在使 UpdateModel 工作时遇到问题,因此我使用了从集合中提取的方法。前 4 个方法调用:orderRepository.FindOrder(id);等给模型原始的编辑。但在这之后我对如何更新子作业有点迷失了。我希望我已经描述了足够多的内容来理解这个问题。
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
Order order = orderRepository.FindOrder(id);
Customer cust = orderRepository.FindCustomer(order.customer_id);
IList<SoilSamplingSubJob> sssj = orderRepository.FindSubOrders(id);
SoilSamplingOrder sso = orderRepository.FindSoilSampleOrder(id);
try
{
UpdateModel(order, collection.ToValueProvider());
UpdateModel(cust, collection.ToValueProvider());
UpdateModel(sso, collection.ToValueProvider());
IList<SoilSamplingSubJob> sssjs = orderRepository.extractSSSJ(collection);
foreach (var sj in sssjs)
UpdateModel(sso, collection.ToValueProvider());
orderRepository.Save();
return RedirectToAction("Details", new { id=order.order_id});
}
catch
{
return View();
}
}
have looked at Phil Haacks project on books at
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
which has been useful, but I have a mix of data types.
I use a modelview so that i can have a mix of objects, in this case: Order (ie order.id, order.date etc), Customer, SoilSamplingOrder and a list of SoilSamplingSubJobs which is like this [0].id, [0].field, [1].id, [1].field etc
Perhaps I should be using ICollection instead of List? I had problems getting UpdateModel to work so I used an extract from collection method. the first 4 method calls : orderRepository.FindOrder(id); etc give the model the original to be edited. but after this point i'm a little lost in how to update the subjobs. I hope i have delineated enough to make sense of the problem.
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
Order order = orderRepository.FindOrder(id);
Customer cust = orderRepository.FindCustomer(order.customer_id);
IList<SoilSamplingSubJob> sssj = orderRepository.FindSubOrders(id);
SoilSamplingOrder sso = orderRepository.FindSoilSampleOrder(id);
try
{
UpdateModel(order, collection.ToValueProvider());
UpdateModel(cust, collection.ToValueProvider());
UpdateModel(sso, collection.ToValueProvider());
IList<SoilSamplingSubJob> sssjs = orderRepository.extractSSSJ(collection);
foreach (var sj in sssjs)
UpdateModel(sso, collection.ToValueProvider());
orderRepository.Save();
return RedirectToAction("Details", new { id=order.order_id});
}
catch
{
return View();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该致力于开发一个视图模型,该模型反映您需要返回的数据,并为该模型创建显示/编辑模板,该模板使用 Phil Haack 的对象列表方法呈现视图模型 - 在本例中为数组子模型类。让模型绑定框架为您的操作构建返回的模型(作为参数),然后从视图模型数据重构您的域模型。 Brad Wilson 有一系列出色的 关于模板的文章应该会有所帮助。
I think you should work on developing a view model that reflects the data that you need to get back and create display/edit templates for that model that renders the view model using Phil Haack's methods for your lists of objects -- in this case, arrays of submodel classes. Let the model binding framework build the returned model (as a parameter) to your action, then reconstitute your domain models from the view model data. Brad Wilson has an excellent series of articles on templating that should be helpful.
我在复杂的类上使用 IModelBinder。您不需要 IModelBinder,但它会让您的控制器后代码块看起来更干净。我现在正在使用 VB,但我的类看起来像这样,例如:
现在我在视图中不使用编辑器模板,因此要让您的列表出现在表单集合中,您必须在视图中添加类似的内容:
当视图被提交/发布时,模型绑定器在命中 mvc post 控制器方法中的第一行代码之前接管。然后,我迭代实际的表单集合并使用正则表达式删除 [#],因为您的表单集合将显示如下列表项: Answers[0].Answer、Answer[0]AnswerId 等:
接下来是 Select Case 代码块。所以你已经知道你的模型中有一个强类型的类,所以你的选择可以如下所示:
I use IModelBinder on my complex classes. You don't need IModelBinder, but it will make your controller post codeblock look much cleaner. I'm using VB at the moment, but my class looks something like this for example:
Now I don't use editor templates in my views, so to have your lists appear in the formcollection you have to add something like this in your view:
When the view is submitted/posted, the model binder takes over before hitting the first line of code in your mvc post controller method. I then iterate through the actual formcollection and strip out the [#] using regex, because your formcollection will show your list items like this: Answers[0].Answer, Answers[0]AnswerId ,etc.:
The Select Case codeblock is next. So you already know you have a strong typed class in your model, so your select can look like this: