Asp.net mvc MVC3不引人注目的Ajax-不反映模型的局部视图
除了在插入对象后返回带有模型的分部时(该模型具有Asp.net mvc MVC3不引人注目的Ajax-不反映模型的局部视图,asp.net-mvc,asp.net-mvc-3,model-view-controller,unobtrusive-javascript,Asp.net Mvc,Asp.net Mvc 3,Model View Controller,Unobtrusive Javascript,除了在插入对象后返回带有模型的分部时(该模型具有NAME=“foo”),它不会使用模型中的值更改名称和百分比文本框外,所有操作似乎都按预期进行 当我通过验证消息在部分的标题中输出@Model.Name时,它正确地显示“foo”。但表单上仍然显示了提交时文本框中的内容 HTML 如果要更改控制器后操作中的值,必须清除modelstate。HTML助手在绑定时首先查看modelstate,然后查看模型。因此: [HttpPost] public ActionResult Create(BeerCre
NAME=“foo”
),它不会使用模型中的值更改名称和百分比文本框外,所有操作似乎都按预期进行
当我通过验证消息在部分的标题中输出@Model.Name
时,它正确地显示“foo”
。但表单上仍然显示了提交时文本框中的内容
HTML
如果要更改控制器后操作中的值,必须清除modelstate。HTML助手在绑定时首先查看modelstate,然后查看模型。因此:
[HttpPost]
public ActionResult Create(BeerCreateModel model)
{
if (ModelState.IsValid)
{
//Add Beer to DB
// Here you are modifying the value of the Name parameter
// in your model. But this parameter is also in ModelState.
// So if you want this change to reflect on the subsequent view you
// need to either clear it from the modelstate
ModelState.Remove("Name");
return PartialView("CreatePartial", new BeerCreateModel { Name = "foo"});
}
else
{
return PartialView("CreatePartial", model);
}
}
仅仅做一个模型有什么副作用吗?Clear()?这个想法是表单将处于模式中,如果成功,我想加载一个空白表单并隐藏模式。@Blankasaurus,是的,您正在擦除整个模型状态以及任何验证错误。如果你不在乎这件事,那么你可以做。
@{
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);
}
}
[HttpPost]
public ActionResult Create(BeerCreateModel model)
{
if (ModelState.IsValid)
{
//Add Beer to DB
// Here you are modifying the value of the Name parameter
// in your model. But this parameter is also in ModelState.
// So if you want this change to reflect on the subsequent view you
// need to either clear it from the modelstate
ModelState.Remove("Name");
return PartialView("CreatePartial", new BeerCreateModel { Name = "foo"});
}
else
{
return PartialView("CreatePartial", model);
}
}