Controller中的Edit方法
[HttpPost]
public ActionResult Edit(Comment comment)
{
if (ModelState.IsValid)
{
db.Entry(comment).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.Users, "UserId", "Email", comment.UserId);
ViewBag.PostId = new SelectList(db.Posts, "PostId", "Title", comment.PostId);
return View(comment);
}
public ActionResult Edit(Comment comment)
{
if (ModelState.IsValid)
{
db.Entry(comment).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.Users, "UserId", "Email", comment.UserId);
ViewBag.PostId = new SelectList(db.Posts, "PostId", "Title", comment.PostId);
return View(comment);
}
View中展示:
<div class="editor-label">
@Html.LabelFor(model => model.UserId, "User")
</div>
<div class="editor-field">
@Html.DropDownList("UserId", String.Empty)
@Html.ValidationMessageFor(model => model.UserId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PostId, "Post")
</div>
<div class="editor-field">
@Html.DropDownList("PostId", String.Empty)
@Html.ValidationMessageFor(model => model.PostId)
</div> @Html.LabelFor(model => model.UserId, "User")
</div>
<div class="editor-field">
@Html.DropDownList("UserId", String.Empty)
@Html.ValidationMessageFor(model => model.UserId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PostId, "Post")
</div>
<div class="editor-field">
@Html.DropDownList("PostId", String.Empty)
@Html.ValidationMessageFor(model => model.PostId)