我在想,也许我正试图实现不可能的事情。我在页面上有一个按钮,显示将项目添加到另一个模型的部分视图。这有其在进一步调查返回null,因此抛出了一个错误下拉列表:在RenderPartial MVC中传递下拉列表
System.InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ProjectId'.
我在控制器
int userId = (int)MySqlWebSecurity.CurrentUserId;
ViewBag.ProjectId = new SelectList(db.ProjectDocuments.Where(a=>a.ProjectUserId=userId), "ProjectId", "ProjectTitle");
的RenderPartial应在模式弹出
呈现使用ViewBag.ProjectId@{Html.RenderPartial("_AddProjectDocument", new ProjectUno.Models.ProjectDocument());}
在_AddProjectDocument.cshtml
<div class="editor-field">
@Html.DropDownList("ProjectId", String.Empty)
@Html.ValidationMessageFor(model => model.ProjectId)
</div>
使用FK和虚拟属性设置了模型。当我在/ ProjectDocument/Create中使用scafolded create view时,按预期填充下拉列表。我将很感激任何帮助。
尝试使用TempData的。 –
Thanks @Mohammadb Batineh是TempData我唯一的选择?我读过使用tempdata不好 – Diin
那么你的dropDown不会被创建?你在哪里使用ViewData? –