ASP.Net MVC 返回具有新模型实例的视图,而不是使用重定向到操作
我有一个使用 ViewModel 的表单视图。新视图由动作“New”呈现。提交此表单后,我将信息发送回“添加”操作。
如果数据库上的所有操作都成功,我想刷新视图。我从“Add”操作内部创建一个新的 viewmodel 实例,并返回 View("New",viewModel) 。这并没有刷新我的观点。所有旧值(已提交)仍保留在视图中。 有没有一种方法可以不使用 RedirectToAction 方法来刷新页面。
我在另一篇文章中读到不应使用 ModelState.Clear,因为它可能会产生不良结果。
谢谢,
三月
编辑 1 - 添加代码
New
public ActionResult New(string id)
{
var sysId= new Guid(id);
.......
........
string Details = pDto.Name + "(" + pDto.Code + ")";
var vm= new ViewModel(id);
vm.Details = Details;
return View(vm);
}
public ActionResult Add(ViewModel vm)
{
ViewModel vm= vm;
if (ModelState.IsValid)
{
var dto= _qRepository.GetFeaturesBy(viewModel.Code);
if (dto!= null)
{
ModelState.AddModelError("Code", "Code is already in Use.");
return View("New", viewModel);
}
_productService.AddFeature(..........);
// ModelState.Clear(); -- this works
vm= new ViewModel(vm.pId) { Message = "Code" + viewModel.Code + " was added ......", Details = vm.Details };
}
return View ("New", vm);
}
I have a form view which uses a ViewModel. A new view is rendered by Action "New". When this form is submitted I send back the info to action "Add".
If all actions on DB are successful I want to refresh the view. I create a new instance of viewmodel from inside "Add" action and do return View("New",viewModel) . This does not refresh my view. All old values ( that were submitted) remains in the view.
Is there a way to refresh the page without RedirectToAction method.
I read on another post that ModelState.Clear should not be used as it can have undesirable result.
Thank you,
Mar
Edit 1 - Code Added
New
public ActionResult New(string id)
{
var sysId= new Guid(id);
.......
........
string Details = pDto.Name + "(" + pDto.Code + ")";
var vm= new ViewModel(id);
vm.Details = Details;
return View(vm);
}
public ActionResult Add(ViewModel vm)
{
ViewModel vm= vm;
if (ModelState.IsValid)
{
var dto= _qRepository.GetFeaturesBy(viewModel.Code);
if (dto!= null)
{
ModelState.AddModelError("Code", "Code is already in Use.");
return View("New", viewModel);
}
_productService.AddFeature(..........);
// ModelState.Clear(); -- this works
vm= new ViewModel(vm.pId) { Message = "Code" + viewModel.Code + " was added ......", Details = vm.Details };
}
return View ("New", vm);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该尝试遵守 post-redirect-get 模式。唯一不这样做的时候是当发布失败并且您想要返回验证错误以便客户可以重新发布表单时。这就是当您从后操作方法返回视图时会发生的情况。所有这些表单值仍然在 ModelState 中浮动,等待在表单上再次使用。
如果您想让用户立即添加另一个项目,您可以使用
RedirectToAction("New")
。另外,听起来这两个操作都应该命名为New
,然后用[HttpPost]
属性和 PostModel 参数来装饰发布版本。如果您在两个不同的操作之间发布和获取,事情会变得混乱,因为您必须记住在 html 表单上显式设置操作,并在验证失败时返回正确的视图。You should try to adhere to the post-redirect-get pattern. The only time you don't is when the post failed and you want to return the validation errors so the client can re-post the form. This is what happens when you return a view from a post action method. All those form values are still floating about in ModelState waiting to be used again on the form.
You can use
RedirectToAction("New")
if you want to let user add another item right away. Also, it sounds like both actions should be namedNew
and then decorate the post version with an[HttpPost]
attribute and the PostModel parameter. Things get messy if you post and get between two different actions because then you'd have to remember to explicitly set the action on the html form and also to return the right view on validation failure.您的模型正在刷新良好。
问题是 HTMLHelpers 在使用您传递下来的新模型之前首先在 ViewState 和 ModelState 中查找数据。
您几乎别无选择,只能清除 ModelState 或自己手动更改 ModelState 条目。
Your model is being refreshed fine.
The problem is the HTMLHelpers look for data in ViewState and ModelState first before using the new model you passed down.
You almost have no choice BUT to clear ModelState or manually change the ModelState entries yourself.
它是在填充值属性的情况下进行渲染的,还是浏览器正在“自动填充”表单?
RedirectToAcction 将引发 302 响应,然后向新表单发出新的“Get”。
It is rendering with the values attributes filled, or the browser is "autofilling" the form?
The RedirectToAcction will cause a 302 response, and then a new "Get" to the new form.