大家好。
我知道这是一个关于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不产生价值?
发布于 2012-06-15 23:16:38
来自客户端的问题是,您的客户端中期望的是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发布于 2012-12-22 13:26:49
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 Sectionhttps://stackoverflow.com/questions/11052331
复制相似问题