I have two instances of an Address.ascx
control in an ASP.NET MVC page.
我在ASP.NET MVC页面中有两个Address.ascx控件实例。
<h1>Shipping Address</h1>
<% Html.RenderPartial("Controls/AddressControl"); %>
<h1>Billing Address</h1>
<% Html.RenderPartial("Controls/AddressControl"); %>
Of course, with the code exactly like this I'll end up with the same IDs for each field in the address. I can easily append a string to the ID of the fields so I'd have 'Street1_billing'
and 'Street1_shipping'
, but I'm not clear how to map this to the model.
当然,使用与此完全相同的代码,我将最终为地址中的每个字段使用相同的ID。我可以很容易地将字符串附加到字段的ID,所以我有'Street1_billing'和'Street1_shipping',但我不清楚如何将其映射到模型。
Whats the best solution to mapping a model to an array of items (in this case only 2). I'm not aware of any ASP.NET 'out of the box' solution for this.
什么是将模型映射到项目数组的最佳解决方案(在这种情况下仅为2)。我不知道任何ASP.NET“开箱即用”的解决方案。
Note: This is slightly similar to this question and I could use this solution from Scott Hanselman, but its not exactly what I want. In my case I know I have two items, so its essentially a 2 item array but I'm wondering if there is a slightly more elegant solution.
注意:这与这个问题有点类似,我可以使用Scott Hanselman的这个解决方案,但这不是我想要的。在我的情况下,我知道我有两个项目,所以它基本上是一个2项目数组,但我想知道是否有一个稍微更优雅的解决方案。
PS. I'm sure this has been asked many times before, but I just cant seem to put in the right search terms. Please link this question if you're aware of dupes!
PS。我确信之前已经多次询问过,但我似乎无法用正确的搜索条件。如果你知道欺骗,请链接这个问题!
2 个解决方案
#1
You can have a ViewModel
您可以拥有ViewModel
OrderCheckoutViewModel
{
public Address ShippingAddress{get;set;}
public Address BillingAddress{get;set;}
}
The values of formelements are mapped to the right Member of the ViewModel if they have the form
如果具有该表单,则formelements的值将映射到ViewModel的右侧成员
<input type="text" name="ShippingAddress.StreetAddress1"><input>
There is no easy and elegant way to come from StreetAddress1 to ShippingAddress.StreetAddress1. My address model has the form:
从StreetAddress1到ShippingAddress.StreetAddress1没有简单而优雅的方法。我的地址模型有以下形式:
public class Address
{
String StreetAddress1 { get; set }
String StreetAddress2 { get; set }
String City { get; set }
String State { get; set }
String Zip { get; set }
String InstanceName{ get; set }
}
I set the InstanceName to the Name of the property (ShippingAddress).
我将InstanceName设置为属性的名称(ShippingAddress)。
Then the form elements are defined in this form
然后以这种形式定义表单元素
<input type="text" name="<%=Model.InstanceName%>.StreetAddress1"><input>
The place of the ascx looks uncommon. Any reason why you dont put it in Shared and access it by
ascx的地方看起来不常见。任何理由你不要把它放在共享和访问它
<% Html.RenderPartial("AddressControl",Model.ShippingAddress); %>
?
#2
First, add an Address class to the model.
首先,将Address类添加到模型中。
public class Address
{
String StreetAddress1 { get; set }
String StreetAddress2 { get; set }
String City { get; set }
String State { get; set }
String Zip { get; set }
}
In Address.ascx, you need a line at the top that inherits the Address model, like this:
在Address.ascx中,您需要在顶部继承Address模型的行,如下所示:
<%@ Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<MyProject.Models.Address>" %>
In the controller for the main view, push your two addresses into the ViewData.
在主视图的控制器中,将两个地址压入ViewData。
Address myAddressObject1 = new Address
{
AddressLine1 = "123 Anywhere Street",
// ..etc. Same with MyAddressObject2. Or, just populate from database.
}
ViewData["Address1"] = myAddressObject1;
ViewData["Address2"] = myAddressObject2;
//
// do other stuff as needed
//
Return View();
In your main view, call your two Address subviews like this:
在主视图中,调用两个Address子视图,如下所示:
<%= Html.RenderPartial("Address", ViewData["Address1"]) %>
<%= Html.RenderPartial("Address", ViewData["Address2"]) %>
#1
You can have a ViewModel
您可以拥有ViewModel
OrderCheckoutViewModel
{
public Address ShippingAddress{get;set;}
public Address BillingAddress{get;set;}
}
The values of formelements are mapped to the right Member of the ViewModel if they have the form
如果具有该表单,则formelements的值将映射到ViewModel的右侧成员
<input type="text" name="ShippingAddress.StreetAddress1"><input>
There is no easy and elegant way to come from StreetAddress1 to ShippingAddress.StreetAddress1. My address model has the form:
从StreetAddress1到ShippingAddress.StreetAddress1没有简单而优雅的方法。我的地址模型有以下形式:
public class Address
{
String StreetAddress1 { get; set }
String StreetAddress2 { get; set }
String City { get; set }
String State { get; set }
String Zip { get; set }
String InstanceName{ get; set }
}
I set the InstanceName to the Name of the property (ShippingAddress).
我将InstanceName设置为属性的名称(ShippingAddress)。
Then the form elements are defined in this form
然后以这种形式定义表单元素
<input type="text" name="<%=Model.InstanceName%>.StreetAddress1"><input>
The place of the ascx looks uncommon. Any reason why you dont put it in Shared and access it by
ascx的地方看起来不常见。任何理由你不要把它放在共享和访问它
<% Html.RenderPartial("AddressControl",Model.ShippingAddress); %>
?
#2
First, add an Address class to the model.
首先,将Address类添加到模型中。
public class Address
{
String StreetAddress1 { get; set }
String StreetAddress2 { get; set }
String City { get; set }
String State { get; set }
String Zip { get; set }
}
In Address.ascx, you need a line at the top that inherits the Address model, like this:
在Address.ascx中,您需要在顶部继承Address模型的行,如下所示:
<%@ Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<MyProject.Models.Address>" %>
In the controller for the main view, push your two addresses into the ViewData.
在主视图的控制器中,将两个地址压入ViewData。
Address myAddressObject1 = new Address
{
AddressLine1 = "123 Anywhere Street",
// ..etc. Same with MyAddressObject2. Or, just populate from database.
}
ViewData["Address1"] = myAddressObject1;
ViewData["Address2"] = myAddressObject2;
//
// do other stuff as needed
//
Return View();
In your main view, call your two Address subviews like this:
在主视图中,调用两个Address子视图,如下所示:
<%= Html.RenderPartial("Address", ViewData["Address1"]) %>
<%= Html.RenderPartial("Address", ViewData["Address2"]) %>