加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用RedirectToAction时,routeValue丢失引用属性

发布时间:2020-12-16 03:32:53 所属栏目:asp.Net 来源:网络整理
导读:所以如果我在第一个控制器中这样做: public class AController:Controller { public ActionResult ActionOne() { MyObject myObj = new MyObject() myObj.Name="Jeff Atwood"; myObj.Age =60; myObj.Address = new Address(40,"Street"); return RedirectTo
所以如果我在第一个控制器中这样做:

public class AController:Controller
    {
            public ActionResult ActionOne()
            {
                 MyObject myObj = new MyObject()
                 myObj.Name="Jeff Atwood";
                 myObj.Age =60;
                 myObj.Address = new Address(40,"Street");

                 return RedirectToAction("ActionTwo","BController",myObj );

             }
    }

在第二个控制器中,myObj会出来,但地址将为空.

public class BController:Controller
        {
                public ActionResult ActionOne(MyObject obj)
                {
                     //obj.Address is null?

                 }
        }

这是预期的吗?任何方式呢?

解决方法

您可以使用 TempData来存储两个请求之间可用的对象.在内部,默认实现使用Session.

public class AController:Controller
{
    public ActionResult ActionOne()
    {
        MyObject myObj = new MyObject()
        myObj.Name = "Jeff Atwood";
        myObj.Age = 60;
        myObj.Address = new Address(40,"Street");
        TempData["myObj"] = myObj;
        return RedirectToAction("ActionTwo","BController");

    }
}

public class BController:Controller
{
    public ActionResult ActionTwo()
    {
        MyObject myObj = TempData["myObj"] as MyObject;
        // test if myObj is defined. If ActionTwo is invoked directly it could be null
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读