C# 没有类型为';IEnumerable<;选择列表项>';那有关键的AnnouncementId
我试图做一个表格,用下拉列表和一个输入文本的框进行更新,但标题中不断出现错误。它直接从create cshtml中复制 Index.cshtml:C# 没有类型为';IEnumerable<;选择列表项>';那有关键的AnnouncementId,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我试图做一个表格,用下拉列表和一个输入文本的框进行更新,但标题中不断出现错误。它直接从create cshtml中复制 Index.cshtml: @model Announcements1.Models.Comment <div id="CommentDiv"></div> <div id="CommentCreateForm"> @using (Ajax.BeginForm("AjaxCreate", "Comments",
@model Announcements1.Models.Comment
<div id="CommentDiv"></div>
<div id="CommentCreateForm">
@using (Ajax.BeginForm("AjaxCreate", "Comments",
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "CommentDiv"
}))
{
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CommentContent, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CommentContent, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CommentContent, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnnouncementId, "AnnouncementId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AnnouncementId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AnnouncementId, "", new { @class = "text-danger" })
</div>
</div>
}
</div>
@section Scripts {
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script src="~/Custom_Scripts/BuildCommentTable.js"></script>
@Scripts.Render("~/bundles/jqueryval")
}
和我的JS文件:
$(document).ready(function () {
$.ajax({
url: '/Comments/BuildCommentTable',
success: function (result) {
$('#CommentDiv').html(result);
}
});
});
首先,您缺少在
BuildCommentTable
操作中填充Annouments DropDownList数据的代码,请在返回视图之前添加它,就像在Post操作中一样
其次,如果将ViewBag键命名为与模型属性相同的名称,则两个操作都应具有ViewBag填充,如:
ViewBag.AnnouncementOptions= new SelectList(db.Announcements, "AnnouncementId", "AnnouncementContent", comment.AnnouncementId);
因此,您的get操作将如下所示:
public ActionResult BuildCommentTable()
{
ViewBag.AnnouncementOptions= new SelectList(db.Announcements, "AnnouncementId", "AnnouncementContent");
var comments = db.Comments.Include(c => c.Announcement);
return PartialView("_CommentTable", GetMyComments());
}
以同样的方式更改后期操作方法ViewBag
总体代码部分:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AjaxCreate([Bind(Include = "CommentId,CommentContent,AnnouncementId")] Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.Add(comment);
db.SaveChanges();
}
ViewBag.AnnouncementOptions = new SelectList(db.Announcements, "AnnouncementId", "AnnouncementContent", comment.AnnouncementId);
return PartialView("_CommentTable", db.Comments.Include(c => c.Announcement));
}
现在在您看来,您应该能够通过以下方式使用强类型助手方法DropDownListFor
:
<div class="col-md-10">
@Html.DropDownListFor(x=>x.AnnouncementId, ViewBag.AnnouncementOptions as SelectList, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AnnouncementId, "", new { @class = "text-danger" })
</div>
@DropDownListFor(x=>x.AnnouncementId,ViewBag.AnnouncementOptions作为SelectList,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.AnnouncementId,“,new{@class=“text danger”})
希望对您有所帮助。您搜索过吗?:@EhsanSajjad是的,但我无法找出问题所在该错误表示
@Html中的AnnouncementId
。DropDownList(“AnnouncementId”,null,…)
为null
谢谢!我得到的唯一问题是buildCommentTable中没有对最后一个参数“comment”的引用。您可以删除它,它是一个可选参数。不幸的是,即使您进行了编辑,我仍然会得到一个错误,@html.dropDownListFor上没有具有键“AnnouncementId”的“IEnumerable”类型的ViewData项。在@html.dropdownlistfor行中
<div class="col-md-10">
@Html.DropDownListFor(x=>x.AnnouncementId, ViewBag.AnnouncementOptions as SelectList, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AnnouncementId, "", new { @class = "text-danger" })
</div>