简体   繁体   中英

MVC Getting model from partial view

I am new to MVC and i was trying to covert one of my webform project.

I have a request page, depending on drop down i select, controls get populated.

There are 10 request types, so i considered using partial view. I will make an ajax request on select change event, and depending on what is selected, i will return the partial view, but when i submit the main page, how will i retrieve the model for partial view, can i retrieve model separately for main page and partial page.

Yu have no way to pass a model from the client side to the server for rendering a partial view. So, no, that's not possible.

As you want to deliver a different partial view depending on the selected item, you need to pass this information to the browser, so that it can decide which action has to invoke to render the view correspondeing to the selected item. To do so, you can add this information as the value of each element int the drop down list. This value can be something as simple as an id or as complex as a whole url with parameters which invoke the required view. (If you choose the url option, you can render it using the Url.Action url helper extension).

Then, in the code that handles the change event, you can recover the value of the selected item, and use it as a parameter for making the ajax call that will render the required partial view. (For example, if it's the url, you can use jQuery.get() or any of the other jQuery ajax methods with that url).

The ideal situation is that you can render the partial view without depending on the previous rednering of the main view. Ie the ideal is that you can build the model for the partial by using the action and parameters received in the ajax call.

But, if you need some information that must be generated when rendering the main view, you can use can use TempData to store it when rendering the main view, and to retrieve it when the partial is rendered. ( TempData or Session depenging on what yu exactly want to do).

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