简体   繁体   English

MVC Partial View呈现但不执行Razor代码

[英]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. 我有一个AJAX调用返回部分视图,但它只返回视图的<div>部分。 My guess is that it's ignoring the Razor code. 我的猜测是它忽略了Razor代码。 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: AJAX调用返回的所有内容是:

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

This is the return statement inside the controller being invoked by the AJAX call (YES, it hits): 这是AJAX调用正在调用的控制器内部的return语句(是,它命中):

return PartialView("MyPartial", MyModel);

I've tried adding @model IEnumerable<MyType> at the top, but that made no difference. 我尝试在顶部添加@model IEnumerable<MyType> ,但这没什么区别。

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. 从我的完整视图来看,我正在调用@Html.Partial("MyPartial", Model)和STILL,只有外部<div>标记会显示在我的页面源代码中。

This is quite a frustrating issue, so any help will be greatly appreciated. 这是一个令人沮丧的问题,因此将不胜感激。

Could it be that the Model collection is empty? 是Model集合为空吗? Try to add another element like @Model.Count inside the div, but outside the foreach. 尝试在div中但在foreach之外添加另一个元素,例如@Model.Count

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 这将测试以下假设:由于空集合而未执行foreach

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

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