如何使用相同的.ascx的两个实例在ASP.NET MVC相同的页面? [英] How to use two instances of the same .ascx in the same page in ASP.NET MVC?

查看:96
本文介绍了如何使用相同的.ascx的两个实例在ASP.NET MVC相同的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC页中的 Address.ascx 控件的两个实例。

I have two instances of an Address.ascx control in an ASP.NET MVC page.

   <h1>Shipping Address</h1>
   <% Html.RenderPartial("Controls/AddressControl"); %>

   <h1>Billing Address</h1>
   <% Html.RenderPartial("Controls/AddressControl"); %>

当然,用code正是这样我会在地址的每个字段相同的ID结束。我可以很容易的字符串追加到字段的ID,所以我不得不'Street1_billing 'Street1_shipping ,但我不太清楚如何将此映射到模型。

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.

请告诉我到模型映射到项目的阵列的最佳解决方案(在此情况下,只有2)。
我不知道任何ASP.NET'开箱即用'解决方案这一点。

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.

注:这是稍微类似这个问题,我可以使用<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow>斯科特Hanselman的这个解决方案,但它不是我想要什么。在我来说,我知道我有两个项目,因此它本质上是一个2项数组,但我不知道是否有一个稍微更优雅的解决方案。

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.

PS。我敢肯定,这已经很多次问过,但我只是不能似乎放在正确的搜索字词。如果你知道愚弄的请链接这个问题!

推荐答案

您可以有一个视图模型

OrderCheckoutViewModel
{
    public Address ShippingAddress{get;set;}
    public Address BillingAddress{get;set;}
}

formelements的值被映射到视图模型的右会员,如果它们具有形式

The values of formelements are mapped to the right Member of the ViewModel if they have the form

<input type="text" name="ShippingAddress.StreetAddress1"><input>

有没有简单而优雅的方式来自StreetAddress1到ShippingAddress.StreetAddress1。
我的地址模型的形式为:

There is no easy and elegant way to come from StreetAddress1 to ShippingAddress.StreetAddress1. My address model has the form:

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 }
}

我的实例名设置为属性的名称(ShippingAddress)。

I set the InstanceName to the Name of the property (ShippingAddress).

然后表单元素都以这种形式定义

Then the form elements are defined in this form

<input type="text" name="<%=Model.InstanceName%>.StreetAddress1"><input>

在ASCX的地方看起来少见。有什么理由你不把它放在共享和

The place of the ascx looks uncommon. Any reason why you dont put it in Shared and access it by

<% Html.RenderPartial("AddressControl",Model.ShippingAddress); %>

这篇关于如何使用相同的.ascx的两个实例在ASP.NET MVC相同的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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