简体   繁体   中英

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

thanks

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM