简体   繁体   中英

MVC Partial View renders but does not execute Razor code

I have an AJAX call returning a partial view, but it only returns the <div> part of the view. My guess is that it's ignoring the Razor code. This seems like an immensely stupid issue and I cannot figure it out for the life of me.

Here's my partial view, in its entirety:

<div id="result">
@foreach (var log in Model)
{
    <br />
    <h3><b>Log ID: </b>@log.ID</h3>
    <h3><b>Log Time: </b>@log.LogTime</h3>
    <h3><b>Event Type: </b>@log.EventType</h3>
    <h3><b>Message: </b>@log.Message</h3>
    <h3><a href="Log/Details/@log.ID">View Details/Delete Entry</a></h3>
}
</div>

All that the AJAX call is returning is:

<div id="result">
</div>

This is the return statement inside the controller being invoked by the AJAX call (YES, it hits):

return PartialView("MyPartial", MyModel);

I've tried adding @model IEnumerable<MyType> at the top, but that made no difference.

On my complete view, I'm calling @Html.Partial("MyPartial", Model) and STILL, only the outer <div> tags are showing up in my page source.

This is quite a frustrating issue, so any help will be greatly appreciated.

Could it be that the Model collection is empty? Try to add another element like @Model.Count inside the div, but outside the foreach.

Alternatively, put a breakpoint in the controller to make sure the collection contains at least one element.

That will test the assumption that the foreach isn't executing due to an empty collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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