ASP.Net MVC返回查看使用模型的新实例,而不是使用重定向到行动 [英] ASP.Net MVC return View with new Instance of Model instead of using Redirect To Action

查看:109
本文介绍了ASP.Net MVC返回查看使用模型的新实例,而不是使用重定向到行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统使用视图模型窗体视图。一个新视图是由行动新建呈现。当这个表单提交我发回的信息,以行动添加。

如果对数据库的所有操作都成功我想刷新视图。我创建一个从里面添加动作视图模型的一个新实例,并做回视图(新建,视图模型)。这不刷新我的看法。所有旧值(已提交)保持在视图中。
有没有办法来刷新不Red​​irectToAction方法的页面。

我读上ModelState.Clear不应该被使用,因为它可以有不良后果另一个岗位。

感谢您,

编辑1 - $ C $下加入

 新 公众的ActionResult新(字符串ID)
        {
            VAR SYSID =新的GUID(ID);
.......
........  字符串详细= pDto.Name +(+ PDTO code +);            VAR VM =新的视图模型(ID);
            vm.Details =详细资料;            返回查看(VM);
        } 公众的ActionResult添加(视图模型VM)
        {
                        视图模型VM虚拟机=;            如果(ModelState.IsValid)
            {                              VAR DTO = _qRepository.GetFeaturesBy(视图模型code);                如果(DTO!= NULL)
                {                    ModelState.AddModelError(code,code已被使用。);                    返回视图(新建,视图模型);
                }                _productService.AddFeature(..........);
               // ModelState.Clear(); - 这工作
                VM =新的视图模型(vm.pId){消息=code+视图模型code +添加......,详细= vm.Details};            }
          返回视图(新建,VM);        }


解决方案

您应该尽量坚持的后重定向,得到格局。你不这样做是唯一的时候,后失败,你想返回验证错误,所以客户端可以重新发布形式。这是当你返回从POST操作方法的看法会发生什么。所有这些表单值仍浮有关的ModelState等待的形式再次使用。

您可以使用 RedirectToAction(新建)如果你想让用户添加其他项目的时候了。此外,它听起来像两个动作应该被命名为,然后装点后的版本与 [HttpPost] 属性,在PostModel参数。如果你发布和两个不同的动作之间获得,因为那时你不得不记住要明确设置HTML表单上的操作,也返回验证失败右视图事情变得复杂。

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);

        }

解决方案

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 named New 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.

这篇关于ASP.Net MVC返回查看使用模型的新实例,而不是使用重定向到行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆