具有两个不同类型的相似类的单个强类型局部视图 [英] Single strongly Typed Partial View for two similar classes of different types

查看:90
本文介绍了具有两个不同类型的相似类的单个强类型局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册主视图,该视图显示两种不同类型的地址1.家庭地址2.邮寄地址

I have a Register Primary View which shows two different types of Addresses 1. Home Address 2. Mailing Address

  public class RegisterModel
     {             
        public AddressModel HomeAddress { get; set; }
        public AddressModel MailAddress { get; set; }
     }

public class AddressModel
{
   public string Street1 { get; set; }
   public string Street2 { get; set; }
   public string State   { get; set; }
   public string City    { get; set; }
}

我的主要注册视图按如下类型强烈地注册到RegisterModel

@model MyNamespace.Models.RegisterModel
@{
     Layout = "~/Views/_Layout.cshtml";
 }
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
{
  <div id="form">
    @Html.Action("MyAddressPartial")
    @Html.Action("MyAddressPartial")  
  </div>
}

MyAddressPartialView如下:-

@model MyNamespace.Models.AddressModel
@{
    Layout = "~/Views/_Layout.cshtml";
}
 <div id="Address">
    @Html.TextBoxFor(m=>m.Street1 ,new { @id="Street1 "})     
    @Html.TextBoxFor(m=>m.Street2,new { @id="Street2"})
    @Html.TextBoxFor(m=>m.State ,new { @id="State "})
    @Html.TextBoxFor(m=>m.City,new { @id="City"})
 </div>

我的RegisterController:-

// Have to instantiate the strongly Typed partial view when my form first loads
// and then pass it as parameter to "Register" post action method. 
// As you can see the @Html.Action("MyAddressPartial") above in main    
// Register View calls this.
public ActionResult MyAddressPartial()
{
   return PartialView("MyAddressPartialView", new AddressModel());
}

我在同一注册控制器中将我的主表单提交到下面提到的操作方法.

I submit my Main Form to below mentioned action method in same Register Controller.

[HttpPost]
public ActionResult Register(RegisterModel model, 
                            AddressModel homeAddress, 
                            AddressModel mailingAddress)
{
       //I want to access homeAddress and mailingAddress contents which should 
       //be different, but as if now it comes same.
}

我不想为MailingAddress和HomeAddress创建一个单独的类.如果这样做,则必须为每个地址创建两个单独的强类型局部视图.

I don't want to create a separate class one for MailingAddress and one for HomeAddress. if I do that then I will have to create two separate strongly typed partial views one for each address.

有关如何重用类和局部视图并使它们动态化并在Action Method Post中读取其单独值的任何想法.

Any ideas on how to reuse the classes and partial views and make them dynamic and read their separate values in Action Method Post.

编辑1回复scott-pascoe:-

在DisplayTemplates文件夹中,我添加了以下AddressModel.cshtml

In DisplayTemplates Folder, I added following AddressModel.cshtml

 <div>
        @Html.DisplayFor(m => m.Street1);
        @Html.DisplayFor(m => m.Street2);
        @Html.DisplayFor(m => m.State);
        @Html.DisplayFor(m => m.City);            
 </div>

也在EditorTemplate文件夹中,我添加了以下AddressModel.cshtml但带有EditorFor

Also In EditorTemplate Folder, I added following AddressModel.cshtml but with EditorFor

 <div>
     @Html.EditorFor(m => m.Street1);
     @Html.EditorFor(m => m.Street2);
     @Html.EditorFor(m => m.State);
     @Html.EditorFor(m => m.City);            
  </div>

现在如何在RegisterView中使用它们,以及如何在Controller的Post Action Method中读取值?还有什么要修改的?我已经在上面添加了几乎全部代码.我是MVC的初学者.

Now how do i use them in RegisterView and also how i read values in Controller's post Action Method ? What else would have to be modified ? I have added almost entire code above. I am pretty beginner to MVC.

推荐答案

典型的ASP.NET MVC方法是对您的自定义类型使用EditorTemplates和DisplayTemplates.

The typical ASP.NET MVC method for doing this is to use EditorTemplates and DisplayTemplates for your custom types.

在〜/Views/Shared中,创建两个文件夹DisplayTemplatesEditorTemplates. 在DisplayTemplates文件夹中,使用模型名称(即(AddressModel))创建局部视图,然后创建DisplayFor模板.

In ~/Views/Shared, Create two folders, DisplayTemplates, and EditorTemplates. In the DisplayTemplates folder create a partial view with the name of your Model, ie (AddressModel), and create a DisplayFor Template.

在EditorTemplates文件夹中,创建另一个名为AddressModel.cshtml的局部视图,并创建一个EditorFor模板.

In the EditorTemplates folder create another partial view named AddressModel.cshtml and create an EditorFor Template.

然后,MVC将自动使用您的模板,并为您提供所需的数据.

MVC will then automatically use your templates and give you the data that you are asking for.

这篇关于具有两个不同类型的相似类的单个强类型局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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