Html.RenderPartial does not give a value

Good afternoon, everyone.

I know this is a pretty simple question from the point of view of MVC, but I can’t get @ Html.RenderPartial for my whole life so as not to give me errors. I use VB.NET and Razor. Most of the examples I found on the Internet are written in C #, which is not difficult for me to convert, but this simple one made me stop. This is in my Index view that _Layout.vbhtml displays:

@Section MixPage @Html.RenderPartial("_MixScreen", ViewData.Model) End Section 

The above expression does not give a value.

Today I searched for quite a while, and the pages on which I take examples look like this:

http://geekswithblogs.net/blachniet/archive/2011/08/03/walkthrough-updating-partial-views-with-unobtrusive-ajax-in-mvc-3.aspx

Getting HTML partial view from inside the controller

Ultimately, what I'm trying to do is return and update the model to a partial view from the controller:

  Function UpdateFormulation(model As FormulationModel) As ActionResult model.GetCalculation() Return PartialView("_MixScreen", model) End Function 

and this controller is called from an expression in javascript:

 function UpdateResults() { jQuery.support.cors = true; var theUrl = '/Home/UpdateFormulation/'; var formulation = getFormulation(); $.ajax({ type: "POST", url: theUrl, contentType: "application/json", dataType: "json", data: JSON.stringify(formulation), success: function (result, textStatus) { result = jQuery.parseJSON(result.d); if (result.ErrorMessage == null) { FillMixScreen(result); } else { alert(result.ErrorMessage); } }, error: function (xhr, result) { alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status); alert("responseText: " + xhr.responseText); } }); } 

If there is a better way to bring this updated model back into view and only update this partial view, I’m all ears. But the premise of these questions is: why does RenderPartial not give value?

+3
jquery asp.net-mvc-3 razor renderpartial vb.net-2010
Jun 15 2018-12-12T00:
source share
2 answers

Well, the problem with the client is that you expect that html not Json in your client, remember that this is Return a view, basically you return a compilation of the view that html changes the data type expected in your html result

 $.ajax({ type: "POST", url: theUrl, contentType: "application/json", dataType: "html", data: JSON.stringify(formulation), success: function (result, textStatus) { result = jQuery.parseJSON(result.d); if (result.ErrorMessage == null) { FillMixScreen(result); } else { alert(result.ErrorMessage); } }, error: function (xhr, result) { alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status); alert("responseText: " + xhr.responseText); } }); 

I also recommend that you use the load method, which is a short version of ajax and always assumes the expected result is html, and it is added to the body of the element you need

Secondly. If you want to load part from your layout, do it like this:

  //note that i'm calling the action no the view @Html.Action("UpdateFormulation","yourController", new { model = model}) //<--- this is code in c# don't know how is in vb 
+1
Jun 15 2018-12-15T00:
source share

Html.RenderPartial records directly the response; it does not return a value. Therefore, you should use it inside a code block.

 @Section MixPage @Code @Html.RenderPartial("_MixScreen", ViewData.Model) End Code End Section 

You can also use Html.Partial () without a block of code to do the same, because Partial () returns a string.

 @Section MixPage @Html.Partial("_MixScreen", ViewData.Model) End Section 
+9
Dec 22 '12 at 5:26
source share



All Articles