我有一些代码似乎没有正常工作。我确信只有一些我不知道的东西,但我已经打了很久的头,并且已经无处可去。部分视图方法返回空(似乎根本不会调用视图)
这里是AJAX调用:
$.ajax({
type: 'POST',
url: '@Url.Action("GetPartial", "MyPage")',
async: true,
data: {
MyID: 'ABC123'
},
error: function (jqXHR, textStatus, errorThrown) {
alert('Error loading partial for ' + MyId + '\n\n' + jqXHR + '-' + textStatus + '-' + errorThrown);
}
}).done(function (result) {
$(data).html(result);
});
这里是MyPageController.cs动作:
[HttpPost]
public PartialViewResult GetPartial(string MyID)
{
return PartialView("ThePartial");
}
在我的意见diretory我有我的页面子目录,其中包含ThePartial.cshtml。
我已经在该cshtml中放置了断点,并且它似乎没有被调用过。
此外,我已经验证PartialView是空的通过稍微改变我的行动代码:
[HttpPost]
public PartialViewResult GetPartial(string MyID)
{
PartialViewResult pView = PartialView("ThePartial");
return pView;
}
,然后把一个断点“返回”行,看PVIEW的内容。
以下是部分视图的缩写版本。
@model MyProject.Models.MyModel // There is a breakpoint here
@{
var x = "testing"; // There is a breakpoint here
x += " 123"; // There is a breakpoint here
}
<div>@x</div>
我实际上在第一行,第三行和第四行上设置了断点,它们从未被命中。
我意识到partial是指一个没有包含在action中的数据模型,但是在实际的版本中使用了model。我从这篇文章中删除了它,以保持简短,认为模型不是问题所在。以下是实际的PartialView调用:
MyData myData = MyModel.GetData(MyID);
PartialViewResult pView = PartialView("ThePartial", myData);
我完全失去了这一点,并希望得到任何帮助。
你可以添加你的部分视图列表吗? – alexmac
我从上面的代码中删除了对数据模型的引用,所以部分视图没什么意义。但我会补充我所能做的。 – ESDictor
调试中的pView是否为空?我可以建议将PartialViewResult更改为ActionResult,并尝试渲染另一个,而不是部分视图(正确显示)。 – alexmac