Html.RenderPartial does not produce a value
Asked Answered
M

2

4

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?

Meanwhile answered 15/6, 2012 at 14:11 Comment(0)
C
1

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
Curtsy answered 15/6, 2012 at 15:16 Comment(0)
U
12

Html.RenderPartial writes directly to the response; it does not return a value. Thus you must use it inside a code block.

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

You could also use Html.Partial() without the code block to do the same thing because Partial() returns a string.

@Section MixPage
    @Html.Partial("_MixScreen", ViewData.Model)
End Section
Unheardof answered 22/12, 2012 at 5:26 Comment(1)
very nice response. This is very clear, and answers the other part of my question. Thanks!Meanwhile
C
1

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
Curtsy answered 15/6, 2012 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.