MVC2 如何编辑多个链接的客户地址

发布于 2024-10-08 00:27:01 字数 639 浏览 1 评论 0原文

客户有帐单和送货地址,因此给定以下数据库模式

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平生欢 2024-10-15 00:27:02

我建议您使用编辑器模板。这样,您就不需要在视图中编写难看的循环,并且帮助器将负责为输入字段生成正确的名称。

因此,在主视图中,不要简单地编写显示的所有代码:

<%: Html.EditorFor(x => x.Addresses) %>

然后为地址创建编辑器模板(~/Views/Home/EditorTemplates/Address.ascx

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<YourApp.Models.Address>" %>    
<%: Html.TextBoxFor(x => x.Address1) %>

注意名称和编辑器模板的位置。该位置应位于 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:

<%: Html.EditorFor(x => x.Addresses) %>

And then create an editor template for an address (~/Views/Home/EditorTemplates/Address.ascx)

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<YourApp.Models.Address>" %>    
<%: Html.TextBoxFor(x => x.Address1) %>

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 the Addresses collection of your model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文