Question

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({
    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:

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

In my Views diretory I have a MyPage subdirectory, which contains ThePartial.cshtml.

I have put breakpoints in that cshtml and it doesn't appear to ever be called.

In addition, I have verified that the PartialView is empty by changing my action code slightly to:

[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.

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. 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:

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

I'm completely lost on this and would appreciate any assistance at all.

Was it helpful?

Solution

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.

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.

OTHER TIPS

Have you tried by specifying the full partial view path to see if it's being hit?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top