【问题标题】:Html.RenderPartial does not produce a valueHtml.RenderPartial 不产生值
【发布时间】:2012-06-18 14:54:56
【问题描述】:

大家好。

我知道这是一个关于 MVC 的非常基本的问题,但我无法终生得到 @Html.RenderPartial 而不给我错误。我正在使用 VB.NET 和 Razor。我在网上找到的大多数示例都是用 c# 编写的,这对我来说并不难转换,但是这个简单的例子让我很难过。这是在我的索引视图中,由 _Layout.vbhtml 呈现:

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

上面的表达式不产生值。

今天早上我找了好久,示例的页面如下:

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

最终,我要做的是从控制器返回并更新模型到局部视图:

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

并且从 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);
        }
    });
}

如果有更好的方法将此更新后的模型返回到视图并仅更新此部分视图,我会全力以赴。但是这个问题的前提是:为什么 RenderPartial 不产生值?

【问题讨论】:

    标签: jquery asp.net-mvc-3 razor vb.net-2010 renderpartial


    【解决方案1】:

    Html.RenderPartial 直接写入响应;它不返回值。因此,您必须在代码块中使用它。

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

    你也可以使用不带代码块的 Html.Partial() 来做同样的事情,因为 Partial() 返回一个字符串。

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

    【讨论】:

    • 非常好的回应。这很清楚,并回答了我问题的另一部分。谢谢!
    【解决方案2】:

    好吧,来自客户端的问题是您期望客户端中的 html 不是 Json,请记住返回视图,基本上您返回的视图编译是 html 更改数据类型预期结果为 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);
        }
    });
    

    我还建议您使用方法load,它是 ajax 的一个简短版本,并始终假定预期结果是一个 html,并且它被附加到您需要的元素的正文中

    第二。如果您想从您的布局中加载部分内容,请这样做

     //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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 2015-10-15
      • 1970-01-01
      相关资源
      最近更新 更多