mvc.net如何编辑嵌套视图模型类 [英] mvc.net how to edit nested viewmodel classes

查看:138
本文介绍了mvc.net如何编辑嵌套视图模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的嵌套ViewModel类...

I have the following nested viewmodel class...

public class CustomerModel
{
  public string name;
  public Address mailingAddress;
  public Address billingAddress;
}

public class Address
{
  public string line1;
  public string city;
  public string country;
}

我希望是有,我试过,看了一些自动化的方式来创建一个编辑页面,但种种迹象表明框架和code-只产生在您的视图模型处理的顶级性能。 '名称'属性是在视图中,并在行动中产生的只有一个,那就是填充了这些地址被保留为空只有'名称'属性。

I was hoping that there is some automated way to create an edit page, but everything that I've tried and read indicates that the framework and code-generates only handle top level properties in your viewmodel. The 'name' property is the only one generated in the view and in the action, it is only the 'name' property that is populated with the addresses being left as null.

[HttpPost]
public ActionResult Edit(CustomerModel model)

不过,如果我手动添加的输入框地址(通过局部视图),并切换到该行动的FormCollection签名,我得到的屏幕上输入相应的地址值。

however, if i manually add input boxes for the address (through partial views) and switch over to the FormCollection signature for the action, i get the appropriate address values entered on the screen.

有没有这个以外创造我自己的函数来的FormCollection转换为CustomerModel任何简单的解决方案?

is there any easy solution for this other than creating my own function to convert from FormCollection to CustomerModel?

推荐答案

你能在这里使用一个编辑器模板?基本上,你创建一个强类型的局部视图(地址是在您的案件的类型),将其存储在特定的文件夹(/查看/共享/ EditorTemplates),每当一个编辑器呈现该数据类型的成员,局部视图反而是自动呈现。因此,呼吁 Html.EditorFor(型号=> model.mailingAddress)。呈现局部视图,而不是

Could you use an editor template here? Basically, you create a strongly typed partial view (Address is the type in your case), store it in a specific folder (/Views/Shared/EditorTemplates) and whenever an editor is rendered for a member of that data type, the partial view is automagically rendered instead. So, calling Html.EditorFor(model => model.mailingAddress) renders the partial view instead.

我想,当我在寻找一些日期时间验证我读到这个首位了。查看<一个href=\"http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx\"相对=nofollow>此链接,也许你的局部视图会有一些 Html.EditorFor(型号=&GT; model.line1) Html.EditorFor(型号=&GT; model.city)

I think the first place I read about this was when I was looking for some DateTime validation. Check out this link, and maybe your partial view will have some Html.EditorFor(model => model.line1)'s and Html.EditorFor(model => model.city)'s

这不会使一切超级自动的,但它的数据类型,如地址今后的编辑帮助。

This does not make everything super-automatic, but it helps with the future editing of data types like Address.

这篇关于mvc.net如何编辑嵌套视图模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆