【发布时间】:2012-01-20 22:04:08
【问题描述】:
一切似乎都按预期工作,除了在插入对象后返回带有NAME = "foo" 模型的部分时,它不会使用模型中的值更改名称和百分比文本框。
当我通过验证消息在部分标题中输出@Model.Name 时,它会正确显示"foo"。但是表单仍然会显示提交时文本框中的内容。
HTML
<div id="createBeerForm">
@{Html.RenderPartial("CreatePartial", new BeerCreateModel());}
</div>
创建部分
@{
AjaxOptions options = new AjaxOptions
{
HttpMethod = "Post",
UpdateTargetId = "createBeerForm",
InsertionMode = InsertionMode.Replace
};
}
@using (Ajax.BeginForm("Create", "Beer", null, options, new { @class = "form-stacked" }))
{
@Html.ValidationSummary(true, "You have errors. Fix them.")
@Html.LabelFor(m => m.Name)
<div>
@Html.TextBoxFor(m => m.Name, new { @class = "xlarge" })
@Html.ValidationMessageFor(m => m.Name)
</div>
@Html.LabelFor(m => m.PercentAlcohol)
<div>
@Html.TextBoxFor(m => m.PercentAlcohol, new { @class = "xlarge" })
@Html.ValidationMessageFor(m => m.PercentAlcohol)
</div>
<p>
<input type="submit" value="Create Beer" />
</p>
}
控制器
[HttpPost]
public ActionResult Create(BeerCreateModel model)
{
if (ModelState.IsValid)
{
//Add Beer to DB
return PartialView("CreatePartial", new BeerCreateModel { Name = "foo"});
}
else
{
return PartialView("CreatePartial", model);
}
}
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 model-view-controller unobtrusive-javascript