ASP.NET MVC 3 如何在创建视图上为具有 ICollection 属性的模型提供多字段创建功能
注意:我使用的是MVC3+Razor、EF4、CF-CTP5
如何让视图能够在客户端上为每个组织动态添加多个地址类,并强绑定到发布时的模型?
如果 (ModelState.IsValid == false),如何让视图解析模型中的值,这样如果您输入 3 个地址并发布无效模型,它会重新填充数字地址及其适当的值?
这是我的模型:
public class Organization
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
...
}
public class Address
{
public int Id { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public int Type { get; set; }
}
我试图弄清楚如何让组织的创建操作 (/Organization/Create) 像这样处理创建(这样地址和电话号码是提交模型的一部分):
[HttpPost]
public ActionResult Create(Organization organization)
{
if (ModelState.IsValid)
{
_db.Organizations.Add(organization);
_db.SaveChanges();
return RedirectToAction("Details", organization.Id);
}
return View(organization);
}
Note: I'm using MVC3+Razor, EF4, CF-CTP5
How can you allow the view to have the ability to add multiple Address classes per Organization dynamically on the client, and bound strongly to the model on post?
How can you have the view parse values in the model if the (ModelState.IsValid == false) such that if you enter 3 addresses and post an invalid model, it re-populates the number addresses and with their appropriate values?
Here are my models:
public class Organization
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
...
}
public class Address
{
public int Id { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public int Type { get; set; }
}
I'm trying to figure out how you can have the Create action for Organization (/Organization/Create) handle the create like thus (such that addresses and phone numbers are part of the submitted model):
[HttpPost]
public ActionResult Create(Organization organization)
{
if (ModelState.IsValid)
{
_db.Organizations.Add(organization);
_db.SaveChanges();
return RedirectToAction("Details", organization.Id);
}
return View(organization);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题相当广泛:)
这只是实现您的要求的方式之一,我相信有比我更好的方式。
我将从你的第二个问题开始:
如果我正确理解你的要求,对我来说这看起来很简单。答案很简单:对视图进行编码以呈现模型类内容,并将无效模型返回给客户端,就像您在
Create
中所做的那样行动。如果您的表单(及其字段)已使用
ValidationSummary/ValidationMessage
html 帮助程序进行修饰,您还将看到验证消息。您可以有一个显示组织属性的主视图,然后有另一个显示相关地址的视图。您可以在此处放置一个超链接或一个按钮,打开一个对话框以添加新地址对象,然后在完成后刷新地址列表。同样,您可以将编辑和删除按钮作为列表上的图标。
地址列表是完全在客户端处理的一个标记,要正确绑定到服务器端模型类,应该为其输入属性遵守一些简单的命名规则。
要使默认模型绑定器类正确绑定您的表单,请在您的
Organization
类中使用以下代码片段要自动绑定,表单的生成代码应如下所示,
其属性名为
Addresses [索引].PropertyName
。如果您在客户端添加新地址,那么这并不重要:只要您的代码遵守此规则,您就可以让默认的 Model Binder 为您完成这项工作。
希望这有帮助
Your question is quite vaste :)
This is just one of the way your requirement can be achieved and I am sure there are better than mine.
I am going to start from your second question:
If I correctly understand your request it looks very simple to me. The answer is simply code your view to render a Model class content and return the invalid model to the client exactly as you are doing in your
Create
action.If your form (and its fields) have been decorated with the
ValidationSummary/ValidationMessage
html helpers, you are going to see also validation messages.You can have a main view showing Organization attributes and then have another view showing related addresses. Here you can place a hyperlink or a button that open a dialog for adding a new address object and then refresh the address list when done. At the same way you can have edit and delete buttons as icons on the list.
The address list is a piece of markup completely handled at client side that, to be correctly binded to the server side Model class should adhere to some simple naming rules for it's input attributes.
To make the Default Model Binder class bind correctly your form use the following snippet for your
Organization
classTo be automatically bindable the resultant code for the form should look as the following
having it's properties named as
Addresses[index].PropertyName
.If you add new addresses on the client it does'nt matter so much: as long as your code respect this rule you can have the default Model Binder do the job for you.
Hope this helps
我不确定我是否正确理解你的问题,但关于问题 1,我认为你正在寻找 ViewModel。也许像这样..
OrganizationViewModel.cs
OrganizationController.cs
Item.cshtml
在我尝试回答第 2 个问题之前,也许您应该指出我是否正确理解了问题 1。希望这会有所帮助。
I'm not sure if I understand your question correctly but with respect to question 1 I think you are looking for a ViewModel. Like this perhaps..
OrganizationViewModel.cs
OrganizationController.cs
Item.cshtml
Before I try and answer number 2 maybe you should indicate whether I am correctly understanding question 1. Hope this helps.
我设法使用 LINQ to SQL 来做到这一点。现在我尝试使用实体框架,但它确实使一切变得更加复杂。所以我没有适合您的解决方案,但也许我的 L2S 解决方案可能有帮助?
使用从我的数据库生成的模型,我可以在我的视图中执行此操作:
我有一个视图模型类:
这工作正常,在我的控制器操作中,我得到了我的联系人对象,它的 Contact.ContactEmailAddresses 列表填充就像我预期的那样。
但对于 EF,我无法再使用从数据库生成的 EmailAddresses 属性上的 [i]。我想出的最好的办法是:
I managed to do this using LINQ to SQL. Now I'm trying to use Entity Framework instead, but it really makes everything more complicated. So I don't have a solution for you, but perhaps my L2S solution might help?
Using models generated from my database I could in my view do this:
I had a view model class:
This worked fine and in my controller action I got my Contact object with it's Contact.ContactEmailAddresses list filled just like I expected.
But with EF, I cannot use the [i] on the EmailAddresses property generated from the database anymore. The best I have come up with is: