MVC2 如何编辑多个链接的客户地址
客户有帐单和送货地址,因此给定以下数据库模式
- Customer(CustomerId)
- Address(AddressId)
- CustomerAddresses(CustomerId,AddressId)
和以下实体框架类,
public class Customer
{
public IEnumerable<Address> Addresses { get; set; }
}
我在视图中输出输入框,如下所示
<% foreach (var address in Model.Addresses) { %>
<%: Html.TextBoxFor(model => address.Address1) %>
<% } %>
当我发布表单时输入 DeliveryAddress1 和 BillingAddress1 后的值,然后迭代 FormCollection 键我得到以下值
客户.地址.地址1 = “送货地址1,账单地址1”
问题是如何区分这两条记录?
A Customer has a billing and delivery address, so given the following database schema
- Customer(CustomerId)
- Address(AddressId)
- CustomerAddresses(CustomerId,AddressId)
And the following Enitity Framework class
public class Customer
{
public IEnumerable<Address> Addresses { get; set; }
}
I output my input boxes in my view like so
<% foreach (var address in Model.Addresses) { %>
<%: Html.TextBoxFor(model => address.Address1) %>
<% } %>
When I post the form values after entering DeliveryAddress1 and BillingAddress1 and then iterate over the FormCollection keys I get the following value
Customer.address.Address1 =
"DeliveryAddress1,BillingAddress1"
The question is how do I distinguish between the two records?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用编辑器模板。这样,您就不需要在视图中编写难看的循环,并且帮助器将负责为输入字段生成正确的名称。
因此,在主视图中,不要简单地编写显示的所有代码:
然后为地址创建编辑器模板(
~/Views/Home/EditorTemplates/Address.ascx
)注意名称和编辑器模板的位置。该位置应位于
EditorTemplates
文件夹中(也可能位于~/Views/Shared/EditorTemplates/Address.ascx
中),并且名称应与名称相同类的地址(地址
)。 ASP.NET MVC 将负责为模型的Addresses
集合的每个元素呈现模板。I would recommend you using editor templates. This way you don't need to write ugly loops in your views and the helpers will take care of generating proper names for the input fields.
So in your main view instead of writing all the code you've shown simply:
And then create an editor template for an address (
~/Views/Home/EditorTemplates/Address.ascx
)Notice the name and location of the editor template. The location should be in the
EditorTemplates
folder (it could also be in~/Views/Shared/EditorTemplates/Address.ascx
) and the name should be the same as the name of the class (Address
). ASP.NET MVC will take care of rendering the template for each element of theAddresses
collection of your model.