ASP.NET MVC Show view after calling Ajax to controller

I have a view with a submit form: when I click on it, the jquery / ajax function is called. This function should encode the view model, call the controller action and show the returned view. Now this is my function:

<script type="text/javascript"> function Analyze() { var urlact = '@Url.Action("Analysis")'; var model = '@Html.Raw(Json.Encode(Model))'; $.ajax({ data: model, type: "POST", url: urlact, dataType: "json", contentType: "application/json; charset=utf-8", success: function (data) { //WHAT HERE?? } }); } </script> 

And the Analysis action is kind of

 public ViewResult Analysis(IEnumerable<Azienda> aziende) { Debug.WriteLine(aziende.Count()); return View(aziende); } 

The return of the species! How can I show that View on success: function (data)? I tried changing the dataType to html and calling alert (data) for success, but I had problems with the encoded model, I tried to comment out the contentType line, but the same problem with the model encoding.

Does anyone know how to do this? The js / query / ajax workaround is fine too.

Thanks everyone!

+4
source share
2 answers

Using

 return PartialView() 

instead

 return View() 

in your controller. Then, in the success function in your ajax call, use the jQuery.html () function to update the element you want to update in your html. See Query.html () and View () and PartialView ()

0
source

Create a separate partial view with aziende as its model and return this to your analysis and then add the result to the div in your view:

 //action returning a partial view public ActionResult Analysis(IEnumerable<Azienda> aziende) { Debug.WriteLine(aziende.Count()); return PartialView("_partialView", aziende); } //then append the result to a div in your javascript success: function (data) { $("#some-div").html(data); } 
0
source

Source: https://habr.com/ru/post/1444581/


All Articles