Single strongly Typed Partial View for two similar classes of different types

﹥>﹥吖頭↗ 提交于 2019-12-13 19:07:28

问题


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

My main Register View is Strongly Typed to RegisterModel as follows

@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 as follows : -

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

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

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.

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

Edit 1 Reply to scott-pascoe:-

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>

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>

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.


回答1:


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

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.

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

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




回答2:


Use @Html.EditorFor (or @Html.DisplayFor, for display) in your view:

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

You will not need to have a separate controller action for the parts, just populate the addresses in the RegisterModel before in your controller. Like this:

[HttpGet]
public ActionResult Register() // this will be the page people see first
{
    var model = new RegisterModel();
    return View(model);  // assuming your view is called Register.cshtml
}


[HttpPost]
public ActionResult Register(RegisterModel model){
    DosomethingWithHomeAddress(model.HomeAddress);
    DosomethingWithMailAddress(model.MailAddress);
    model.IsSaved = true; // some way to let the user knwo that save was successful;
                          // if this is true, display a paragraph on the view
    return View(model);
}


来源:https://stackoverflow.com/questions/16344503/single-strongly-typed-partial-view-for-two-similar-classes-of-different-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!