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

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.

Answers


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.


Need Your Help

velocity.js progress callback function not working

javascript velocity.js

I have used velocity.js to animate something on page but in progress callback function does not working for me. I am getting null value for the tweenValue para.

How to merge parent content inside his first child with jQuery

jquery merge parent-child wrap textnode

I'm not able to figure out how to merge parent content inside his first child with jQuery.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.