How can i dynamically load a Partial View?
I mean I have this view, lets say ListProducts
, there I select some dropdownlists with products, etc, and with the selected values from those I wanna fill a partial view, which would be in a div that was invisible but after onchange()
event would become visible and with the data from the specific selected items.
You can use ajax to call action an then just insert html string using jQuery to the page where you want it to appear:
Server-side:
Render partial view to string Renders partial view on server to html string, useful when you need to add partial view to ASP.NET MVC page via AJAX.
Client-side:
The following article tells you how to do it with minimum javascript. Basically you return html instead of JSON to your response object.
https://www.simple-talk.com/content/article.aspx?article=2118
Use Ajax :)
http://api.jquery.com/jQuery.ajax/
Example:
Use jQuery's $.load() with a controller action that returns a partial view.
For example:
HTML
Controller
View
I believe you can do something like this example, just using the change event on your dropdown instead. It's a simple jQuery call, you can find more on the jQuery website.
The first parameter is the view you need to call for the details.
The second parameter is the selected value.
The third parameter of the $.load function is the callback function, where you can parse the result and do what you need to do.
If you have a multiple select $(this).val() that will give you an array with the selected options.
If you want only return a Json object you may want to follow this example.
You can do this by following these steps. In your controller, you return a partial view.
then in the view you have an empty div
and then load the partial view using jQuery:
Hope this helps