我的行动很简单:
[HttpPost]
public virtual ActionResult New(Feedback feedback)
{
feedback.CreatedDate = DateTime.UtcNow;
if (TryValidateModel(feedback))
{
FeedbackRepository.Add(feedback);
var model = new Feedback
{
SuccessfullyPosted = true
};
return PartialView(MVC.Shared.Views._FeedBackForm, model);
}
return PartialView(MVC.Shared.Views._FeedBackForm, feedback);
}
因此,想法是如果接收的数据正在验证罚款,则返回具有空反馈实体的部分视图。 事实是,如果我看一下萤火虫的反应,我会看到旧的价值回归,这有多奇怪?
表格如下:
@using (Ajax.BeginForm(MVC.Feedback.New(), new AjaxOptions { UpdateTargetId = "contactsForm", HttpMethod = "post" })) {
@Html.LabelFor(x => x.FirstName) @Html.EditorFor(x => x.FirstName) @Html.ValidationMessageFor(x => x.FirstName)
@Html.LabelFor(x => x.LastName) @Html.EditorFor(x => x.LastName) @Html.ValidationMessageFor(x => x.LastName)
@Html.LabelFor(x => x.Email) @Html.EditorFor(x => x.Email) @Html.ValidationMessageFor(x => x.Email)
@Html.LabelFor(x => x.Phone) @Html.EditorFor(x => x.Phone) @Html.ValidationMessageFor(x => x.Phone)
@Html.LabelFor(x => x.Comments)
if (Model.SuccessfullyPosted) { Feedback sent successfully. }
@Html.TextAreaFor(x => x.Comments, new { cols = 60, rows = 10 }) @Html.ValidationMessageFor(x => x.Comments)}
是否有可能以某种方式禁用此行为以及PartialView(MVC.Shared.Views._FeedBackForm, model)
如何设法获得不同的模型?
更新:我看到stackoverflow从视图中获取所有html,但无法找到解决方法。
答案 0 :(得分:3)
ModelState
是模型值的主要供应商。即使您将模型传递给View
或PartialView
,EdiorFor
也会首先查看ModelState
相应的属性值,如果它不存在,则只会进入模型本身。发布到控制器时会填充ModelState(旧反馈)。即使您创建新的反馈并将其作为模型传递,ModelState
已包含先前发布的反馈的值,因此您在客户端上获取旧值。在成功发布结果之前清除模型状态将对您有所帮助。
FeedbackRepository.Add(feedback);
var model = new Feedback
{
SuccessfullyPosted = true
};
ModelState.Clear(); // force to use new model values
return PartialView(MVC.Shared.Views._FeedBackForm, model);