简体   繁体   English

PartialView方法返回Null(似乎根本没有调用视图)

[英]PartialView Method returns Null (doesn't seem to call the view at all)

I have some code that doesn't seem to be working correctly. 我有一些似乎无法正常工作的代码。 I'm sure that there is just something I'm missing, but I've beaten my head against it too long already and am getting nowhere. 我确定我只是想念一些东西,但是我已经把头撞得太久了,而且一无所获。

Here is the AJAX call: 这是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);
});

Here is the action in MyPageController.cs: 这是MyPageController.cs中的操作:

[HttpPost]
public PartialViewResult GetPartial(string MyID)
{
    return PartialView("ThePartial");
}

In my Views diretory I have a MyPage subdirectory, which contains ThePartial.cshtml. 在“视图”目录中,我有一个MyPage子目录,其中包含ThePartial.cshtml。

I have put breakpoints in that cshtml and it doesn't appear to ever be called. 我已经在该cshtml中放置了断点,而且似乎从未被调用过。

In addition, I have verified that the PartialView is empty by changing my action code slightly to: 另外,通过将操作代码稍作更改为:我已验证PartialView为空:

[HttpPost]
public PartialViewResult GetPartial(string MyID)
{
    PartialViewResult pView = PartialView("ThePartial");
    return pView;
}

and then putting a breakpoint on the "return" line and looking at the contents of pView. 然后在“返回”行上放置一个断点,然后查看pView的内容。

Following is an abbreviated version of the partial view. 以下是部分视图的缩写形式。

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

I actually have breakpoints set on the first, third, and fourth lines that have never been hit. 实际上,我在从未命中过的第一行,第三行和第四行上设置了断点。

I realize that the partial refers to a data model that is not included in the action, but in the actual version the model IS used. 我意识到,部分引用是指未包含在操作中的数据模型,但是在实际版本中是使用IS模型。 I removed it from this post to keep it short, thinking that the model wasn't where the issue was. 为了简短起见,我从这篇文章中删除了它,以为模型不是问题所在。 Here is the actual PartialView call: 这是实际的PartialView调用:

MyData myData = MyModel.GetData(MyID);
PartialViewResult pView = PartialView("ThePartial", myData);

I'm completely lost on this and would appreciate any assistance at all. 我对此完全迷失了,不胜感激。

It appears that there was a syntax error of some sort in the partial view, and that the error is not reported in any way except to not return the view. 似乎在局部视图中存在某种语法错误,并且除了不返回视图以外,没有以任何方式报告该错误。

To come to this conclusion I did the following: 为了得出这个结论,我做了以下工作:

I created a simple partial view, consisting of: 我创建了一个简单的局部视图,包括:

<div>THIS IS A TEST</div>

Then I had my Action return this view, and it did so successfully. 然后我让Action返回此视图,并成功完成了此操作。

Then I copied blocks of code from the old (complete) partial view to the new one, and at one point it stopped returning anything. 然后,我将代码块从旧的(完整的)局部视图复制到了新的局部视图,并在某一时刻停止返回任何内容。 I am now going through the code to narrow down where the error is. 我现在正在检查代码以缩小错误的位置。

I have to say that I'm very unhappy that the error was not reported in any way. 我不得不说我很不高兴没有以任何方式报告该错误。

Thank you everyone who contributed their thoughts here. 谢谢大家在这里发表了自己的想法。

您是否尝试通过指定完整的局部视图路径来查看是否被击中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM