[ChildActionOnly]
public ActionResult CommentForm(int bookid)
{
var model = new CommentSurrogate();
model.BookID = bookid;
model.Date = DateTime.Now;
return PartialView("_CommentForm", model);
}
[HttpPost]
[ChildActionOnly]
public ActionResult CommentForm(CommentSurrogate model)
{
if (ModelState.IsValid)
{
if (Session["commentSec"] as string == model.SecurityCode)
{
model.Date = DateTime.Now;
CommentRepository repo = new CommentRepository();
repo.Save(model);
ViewBag.CommentMessage = "Yorumunuz başarıyla kaydedilmiştir. Editörlerimiz tarafından incelenip onaylandıktan sonra yayınlanacaktır.";
return PartialView("_CommentForm", new CommentSurrogate {Date = DateTime.Now, BookID= model.BookID });
}
else
ModelState.AddModelError("SecurityCode","Güvenlik kodunu yanlış girdiniz.");
}
return PartialView("_CommentForm", model);
}
BookDetailView中的
@Html.Action("CommentForm", "Books", new { bookid = Model.ID })
当我用评论表写评论和发帖时。使用[HttpPost]属性调用CommentForm操作。而且我正在保存模型,此时没问题。
return PartialView("_CommentForm", new CommentSurrogate {Date = DateTime.Now, BookID= model.BookID });
但_CommentForm似乎没有重新获得上面代码传递的新的CommentSurrogate模型。发布后的评论表格仍然包含当前发布数据的数据。
_CommentForm.cshtml
@model Yayinevi.Data.Surrogates.CommentSurrogate
@using (Html.BeginForm()){
@Html.AntiForgeryToken()
<div id="respond" class="clearfix">
<div class="title-block" style="margin: 0 0 30px 0;">
<h3>Kitap Hakkında Yorum Yazın</h3>
</div>
@if (@ViewBag.CommentMessage != null)
{
<div class="msg success"><p>@ViewBag.CommentMessage</p></div>
}
@Html.HiddenFor(model => model.BookID)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="field-row">
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "text_input" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger", style = "display:block;margin-top:-20px;margin-bottom:15px;" })
</div>
<div class="field-row">
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "text_input" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger", style = "display:block;margin-top:-20px;margin-bottom:15px;" })
</div>
<div class="field-row">
@Html.LabelFor(model => model.Comment)
@Html.TextAreaFor(model => model.Comment, new { htmlAttributes = new { @class = "text_input", cols = "60", rows = "9" } })
@Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger", style = "display:block;margin-top:-20px;margin-bottom:15px;" })
</div>
<div class="field-row">
@Html.LabelFor(model => model.SecurityCode)
<img style="vertical-align:middle;" src="@(Url.Action("Captcha", "Layout", new { SessionName="commentSec"}))" /> <br />@Html.EditorFor(model => model.SecurityCode, new { htmlAttributes = new { @class = "text_input" } })
@Html.ValidationMessageFor(model => model.SecurityCode, "", new { @class = "text-danger", style = "display:block;margin-top:-20px;margin-bottom:15px;" })
</div>
<input class="button1" type="submit" value="Gönder" id="submit" name="submit">
</div>
}