MVC RedirectToAction()的方式来传递对象移动到目标的行动? [英] MVC RedirectToAction() any way to pass object to the target action?

查看:700
本文介绍了MVC RedirectToAction()的方式来传递对象移动到目标的行动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在人口catList总是计数= 0时,code跳转到CreateProduct(),所以我把它它不会提供。
考虑RouteValueDictionary不这样做呢?任何其他方式?

 公众的ActionResult GetCats(INT CATID)
    {        清单<类别及GT; catList =新的List<类别及GT;();        如果(CATID> = 0)
        {            catList = context.Categories.Where(X => x.PCATID == CATID).ToList();
        }
        返回RedirectToAction(CreateProduct,管理,新的{catList});
    }公众的ActionResult CreateProduct(列表<分类和GT; catList){}


解决方案

您实际上是试图用控制器来进行数据访问。

移动GetCats数据检索到业务层(Service对象,仓库,无论适合你)。

然后,CreateProduct将需要有两次(2签名)。一个不带参数中,你会从你的业务层称之为GetCats,并将其发送到视图。

另外的实施将要被标记的HttpPostAttribute并将在参数包含所有必要的信息来创建一只猫。

这就是全部。简单和容易。

The populated catList is always Count=0 when the code jumps to CreateProduct() so I take it it does not get delivered. Considering RouteValueDictionary does not do this ? Any other way?

       public ActionResult GetCats(int CatID)
    {

        List<Category> catList = new List<Category>();

        if (CatID >= 0 )
        {

            catList = context.Categories.Where(x => x.PCATID == CatID).ToList();
        }


        return RedirectToAction("CreateProduct", "Admin", new { catList });
    }





public ActionResult CreateProduct(List<Category> catList) {      }

解决方案

You are actually trying to use controllers to do data access.

Move the "GetCats" data retrieval into your business layer (Service object, Repository, whatever suits you).

Then, CreateProduct will need to be there twice (2 signatures). One with no parameters in which you are going to call "GetCats" from your business layer and send it to the view.

The other implementation is going to be the flagged with the HttpPostAttribute and will contain in parameters all the necessary information to create a cat.

That's all. Simple and easy.

这篇关于MVC RedirectToAction()的方式来传递对象移动到目标的行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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