Search code examples
jqueryasp.net-mvc-3razorvb.net-2010renderpartial

Html.RenderPartial does not produce a value


Good day, all.

I know that this is a pretty basic question in terms of MVC, but I can not for the life of me get @Html.RenderPartial to not give me errors. I am using VB.NET, and Razor. Most examples that I have found online are written in c#, which isn't hard for me to convert, but this simple one has got me stumped. This is in my Index view, that is being rendered by the _Layout.vbhtml:

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

The above expression does not produce a value.

I have looked for quite a while this morning, and the pages that I am taking examples from are as follows:

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

Getting a Partial View's HTML from inside of the controller

Ultimately, what I am trying to do is return and updated model to a partial view from the controller:

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

and that controller is being 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 return this updated model to the view and only update this partial view I am all ears. But the premise of this questions is: Why does RenderPartial not produce a value?


Solution

  • Well, the problem from the client it's that you're expecting an html not a Json in your client, remember that that Return a view, basically you're returning the view compile which's the html change the datatype expected in your result to html

    $.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);
        }
    });
    

    Also I recommend you that use the method load, which is a short version of the ajax and assumes always that the expected result it's a html and it;s appended to the body of the element that you need

    Second. If you want to load the partial From your layout do it like this

     //note's 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