Html.RenderPartial does not produce a value

六眼飞鱼酱① 提交于 2020-01-04 04:11:15

问题


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?


回答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



回答2:


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


来源:https://stackoverflow.com/questions/11052331/html-renderpartial-does-not-produce-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!