将模型与局部视图一起使用 - 当前上下文中不存在该名称
Using Model with a Partial View - The name does not exist in the current context
这里又是一个书呆子菜鸟。我已经完成了示例 nerddinner 项目,并开始创建我需要的新的类似东西。
所以,我的 index.cshtml 看起来像这样:
@model PagedList.IPagedList<ProjectSender.Models.Incident>
...
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink(item.Title, "Details", new { id = item.IncidentId })
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.Partial("_ReserveStatusList", item)
</td>
</tr>
}
我的局部视图是这样的:
@model PagedList.IPagedList<ProjectSender.Models.Incident>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script type="text/javascript">
function AnimateProjectMessage() {
$("#projectmsg").animate({ fontSize: "1.5em" }, 400);
}
</script>
<div id="projectmsg">
@Ajax.ActionLink("Reserve this Project",
"Reserve", "Projects",
new { id = item.IncidentId },
new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "projectmsg", OnSuccess = "AnimateProjectMessage" })
</div>
如果我替换部分视图并只使用 index.cshtml
中的 Ajax.ActionLink
,它就可以正常工作。但是,如果我在局部视图中使用它,对于 item (in item.IncidentId)
我得到 "The name item does not exist in the current context." 但是,VS 中没有红色波浪线。我在浏览器中收到此错误。
如果您将其与 nerddinner 进行比较,我基本上是在尝试将每顿晚餐的 RSVP 选项删除到晚餐列表中。 RSVP 部分视图通常在详细信息页面上。
编辑: 为了完整起见。这是我要求的控制器代码,以防将来有人看到它:
public ActionResult Index(string searchString, int? page)
{
int pageSize = 10;
int pageNumber = (page ?? 1);
IQueryable<Incident> incidents = null;
incidents = incidentRepository.FindRecentIncidents(searchString);
return View(incidents.ToPagedList(pageNumber, pageSize));
}
最终,从 MVC3/EF4 升级到 MVC4/EF4 并将局部视图放回原始的 nerddinner 代码(如已接受的答案中所建议的),一切正常。
请注意,部分未定义 item
。部分视图将其作为模型接收(将您的部分模型固定为单个 Incident
顺便说一句 - 您没有在其中传递列表),因此您需要使用 Model
来引用传递的项目相反:
@model ProjectSender.Models.Incident
...
@Ajax.ActionLink("Reserve this Project",
"Reserve", "Projects",
new { id = Model.IncidentId },
这里又是一个书呆子菜鸟。我已经完成了示例 nerddinner 项目,并开始创建我需要的新的类似东西。
所以,我的 index.cshtml 看起来像这样:
@model PagedList.IPagedList<ProjectSender.Models.Incident>
...
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink(item.Title, "Details", new { id = item.IncidentId })
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.Partial("_ReserveStatusList", item)
</td>
</tr>
}
我的局部视图是这样的:
@model PagedList.IPagedList<ProjectSender.Models.Incident>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script type="text/javascript">
function AnimateProjectMessage() {
$("#projectmsg").animate({ fontSize: "1.5em" }, 400);
}
</script>
<div id="projectmsg">
@Ajax.ActionLink("Reserve this Project",
"Reserve", "Projects",
new { id = item.IncidentId },
new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "projectmsg", OnSuccess = "AnimateProjectMessage" })
</div>
如果我替换部分视图并只使用 index.cshtml
中的 Ajax.ActionLink
,它就可以正常工作。但是,如果我在局部视图中使用它,对于 item (in item.IncidentId)
我得到 "The name item does not exist in the current context." 但是,VS 中没有红色波浪线。我在浏览器中收到此错误。
如果您将其与 nerddinner 进行比较,我基本上是在尝试将每顿晚餐的 RSVP 选项删除到晚餐列表中。 RSVP 部分视图通常在详细信息页面上。
编辑: 为了完整起见。这是我要求的控制器代码,以防将来有人看到它:
public ActionResult Index(string searchString, int? page)
{
int pageSize = 10;
int pageNumber = (page ?? 1);
IQueryable<Incident> incidents = null;
incidents = incidentRepository.FindRecentIncidents(searchString);
return View(incidents.ToPagedList(pageNumber, pageSize));
}
最终,从 MVC3/EF4 升级到 MVC4/EF4 并将局部视图放回原始的 nerddinner 代码(如已接受的答案中所建议的),一切正常。
请注意,部分未定义 item
。部分视图将其作为模型接收(将您的部分模型固定为单个 Incident
顺便说一句 - 您没有在其中传递列表),因此您需要使用 Model
来引用传递的项目相反:
@model ProjectSender.Models.Incident
...
@Ajax.ActionLink("Reserve this Project",
"Reserve", "Projects",
new { id = Model.IncidentId },