2012-06-15 52 views
3

美好的一天,所有。Html.RenderPartial不会产生值

我知道这是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不会产生一个值?

回答

1

嗯,客户端的问题是你期望在你的客户端不是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和它; S追加到元素的身体你需要

二。如果你想加载部分从你的布局这样做这样做

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

完美!谢谢! – AaronBastian

9

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 
+0

非常好的回应。这很清楚,并回答了我的问题的其他部分。谢谢! – AaronBastian