【问题标题】:Passing complex object with RouteValueDictionary using RedirectToAction使用 RedirectToAction 通过 RouteValueDictionary 传递复杂对象
【发布时间】:2014-06-19 06:36:37
【问题描述】:

我正在尝试使用 RedirectToAction 方法将复杂对象(对象内的对象)传递给我的操作方法,但它返回 null。有没有办法使用 RouteValueDictionary 做到这一点?

我的模特:

public class ModelUser
    {
        public UserLine User { get; set; }
    }

    public class UserLine
    {

        public int? UserID { get; set; }

        public string FirstName { get; set; }

        public string MiddleName { get; set; }

        public string LastName { get; set; }

        public string Email { get; set; }

}

我的动作方法:

[HttpPost]
        public async Task<ActionResult> Create(UserCreate model)
        {
            var valDTORegister = new RegisterLine() { Email = model.Email, Password = model.Password, OwnerType = model.OwnerType };
            var result = await PostRequest<RegisterLine, UserLine>("http://localhost:10853/", "api/user/Create", valDTORegister);

            var usermodel = new ModelUser();
            usermodel.User = result;

            return RedirectToAction("ProfileUser", new RouteValueDictionary(usermodel));

        }

        public ActionResult ProfileUser(ModelUser usermodel) //User object is null
        {
            return View();
        }

我尝试使用 RouteValueDictionary 仅传递 UserLine 对象,并且值已正确传递给我的 ProfileUser 方法。这很好,但我想传递整个 ModelUser 对象,因为我可能需要在其中添加更多对象。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:


    您可以将ProfileUser 页面连接到模型ModelUser,然后像这样调用这个视图:

    return View("ProfileUser", usermodel);
    

    动作应该没有参数

    public ActionResult ProfileUser()
    {
       return View();
    }
    

    并且所有数据都在 ProfileUser 的模型(“ModelUser”)中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-25
      • 2011-01-31
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多