asp.net mvc - Single strongly Typed Partial View for two similar classes of different types -
i have register primary view shows 2 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 typed registermodel 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 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 instantiate typed partial view when form first loads // , pass parameter "register" post action method. // can see @html.action("myaddresspartial") above in main // register view calls this. public actionresult myaddresspartial() { return partialview("myaddresspartialview", new addressmodel()); }
i submit main form below mentioned action method in same register controller.
[httppost] public actionresult register(registermodel model, addressmodel homeaddress, addressmodel mailingaddress) { //i want access homeaddress , mailingaddress contents should //be different, if comes same. }
i don't want create separate class 1 mailingaddress , 1 homeaddress. if have create 2 separate typed partial views 1 each address.
any ideas on how reuse classes , partial views , make them dynamic , read separate values in action method post.
edit 1 reply scott-pascoe:-
in displaytemplates folder, 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, added following addressmodel.cshtml 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 use them in registerview , how read values in controller's post action method ? else have modified ? have added entire code above. pretty beginner mvc.
the typical asp.net mvc method doing use editortemplates , displaytemplates custom types.
in ~/views/shared, create 2 folders, displaytemplates
, , editortemplates
. in displaytemplates folder create partial view name of model, ie (addressmodel), , create displayfor
template.
in editortemplates folder create partial view named addressmodel.cshtml
, create editorfor
template.
mvc automatically use templates , give data asking for.
Comments
Post a Comment