2012-06-15 51 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