I have my views render to route requests like /controller/action (for instance, /Page/Create), so pretty classic.
I am creating a form where view will be loaded through AJAX request with jQuery get(), so essentially I need my Views or to be precise, my controller actions, to return plain HTML string when an AJAX request is made to the route that renders the View. I want to use the same views as for normal requests, so I cannot afford to create any new views.
It is something like calling RenderPartial(“MyViewUserControl”) but from inside the JS code.
You need to move the HTML strings to partial views, then include the partial views in your regular views (if applicable) and render the partial views in your actions.
You’ll probably want to use the
Request.IsAjaxRequest()extension method in the actions.To get the HTML in Javascript, you can make a normal AJAX request to the action’s URL.