Tutorial :partial views in ASP.NET MVC?


I have a three database tables related for example: company( one - one) Contact( one- one) Address,

I need to create a company, then create contact for company, then create an address for contact in one page (to make things easier for user).

the user doenst have to fill all the details at once and submit it, he may create company today, then add contact tomorrow and then after tomorrow edit the company details and etc..the actions are randoms by the users...

there are many ways, but what is the best way to achieve this using ASP.NET MVC??



IMHO, the best way would be to have a PartialView for each table and for the View to have a form view model that has each table object as a property

FormViewModel      Company company {get;set;}      Contact contact {get;set;}      address address {get;set;}  

You would return the above model to the view in the controller return View(FormViewModel);

Then when you render each partial you pass in the appropriate model.

Html.RenderPartial("ContactEntry", Model.contact);  

When you submit the form you then do a TryUpdateModel to grab the values and begin saving to your data layer.

EDIT In response to Robert

Sam, you'd also need to ensure that either fields are enabled / disabled as data is filled in. For example you can't have a contact w/out a company first.

You could, if you got fired up enough, check for data as it's filled in. So as the user completes the form you activate fields w/out saving first. You can do this with jQuery and even client side validators that you can write in MVC.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »