简体   繁体   English

重定向到动作传递复杂 model

[英]Redirect to action pass complex model

I want to pass to RedirectToAction model with List type property我想通过 List 类型属性传递给 RedirectToAction model

For example, I have this simple model:例如,我有这个简单的 model:

public class OrgToChooseFrom
    {
        public string OrgId { get; set; }
        public string FullName { get; set; }
    }

And complex model as this:和复杂的 model 一样:

public class SelectCounteragentViewModel
    {
        public List<OrgToChooseFrom> Counteragents { get; set; }
        public OrgToChooseFrom SelectedOrg { get; set; }
    }

When I pass simple model with RedirectToAction every value is in place当我通过带有 RedirectToAction 的简单 model 时,每个值都到位

        [HttpGet]
        public IActionResult ConfirmChoice(OrgToChooseFrom vm)
        {
            return View(vm);
        }

But when I try to pass complex model SelectCounteragentViewModel, there are empty list and null for the "SelectedOrg" field但是当我尝试传递复杂的 model SelectCounteragentViewModel 时,“SelectedOrg”字段有空列表和 null

        [HttpGet]
        public IActionResult SelectFromCAOrganizations(SelectCounteragentViewModel vm)
        {
            return View(vm);
        }

How can I do it?我该怎么做?

RedirectToAction cannot pass complex model.You can try to use TempData as Kiran Joshi said.Here is a demo: RedirectToAction无法传递复杂的TempData 。您可以尝试使用Kiran Joshi所说的 TempData。这是一个演示:

public IActionResult Test()
        {
           
            SelectCounteragentViewModel vm = new SelectCounteragentViewModel { Counteragents = new List<OrgToChooseFrom> { new OrgToChooseFrom { OrgId ="1", FullName = "d" } }, SelectedOrg = new OrgToChooseFrom { OrgId = "1", FullName = "d" } };
            TempData["vm"] = JsonConvert.SerializeObject(vm);
            return RedirectToAction("SelectFromCAOrganizations", "ControllerName");
        }
[HttpGet]
        public IActionResult SelectFromCAOrganizations()
        {
            SelectCounteragentViewModel vm = JsonConvert.DeserializeObject<SelectCounteragentViewModel>(TempData["vm"].ToString());
            return View(vm);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM