我正在尝试使用下拉列表和一个用于输入文本的框来更新表,但是仍然在标题中出现错误。它直接从create cshtml中复制。
Index.cshtml:
@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")
}
CommentController:
public ActionResult BuildCommentTable()
{
var comments = db.Comments.Include(c => c.Announcement);
return PartialView("_CommentTable", GetMyComments());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AjaxCreate([Bind(Include = "CommentId,CommentContent,AnnouncementId")] Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.Add(comment);
db.SaveChanges();
}
ViewBag.AnnouncementId = new SelectList(db.Announcements, "AnnouncementId", "AnnouncementContent", comment.AnnouncementId);
return PartialView("_CommentTable", db.Comments.Include(c => c.Announcement));
}
和我的JS文件:
$(document).ready(function () {
$.ajax({
url: '/Comments/BuildCommentTable',
success: function (result) {
$('#CommentDiv').html(result);
}
});
});
答案 0 :(得分:1)
首先,您缺少在BuildCommentTable
操作中填充Annoucements DropDownList数据的代码,在返回View之前添加它,就像在Post操作中一样。
其次,将ViewBag键命名为与Model属性相同,您的两个操作都应该具有ViewBag的范围:
ViewBag.AnnouncementOptions= new SelectList(db.Announcements, "AnnouncementId", "AnnouncementContent", comment.AnnouncementId);
所以你的行动就像:
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>
希望它对你有所帮助。