How do return Page with a model in ASP.Net Core Razor Page

后端 未结 1 1363
执笔经年
执笔经年 2021-01-29 13:07

How to I Redirect to a Page and Passing its model? Just like what we have in MVC return View(model: MyModel);

What i have tried: return RedirectToP

相关标签:
1条回答
  • 2021-01-29 13:40

    MVC has built in dictionary object TempData. You can serialize your model, put JSON string into TempData and then on the redirected action you can get and deserialize JSON string into object.

    public ActionResult Create(Booking item)
    {
        TempData["data"] = JsonConvert.SerializeObject(MyModel);
        return RedirectToAction("Details", new { id = 1 });
    }
    

    On other action

    public ActionResult Details(int id)
    {
        object o;
        TempData.TryGetValue("data", out o);
        var MyModel = JsonConvert.DeserializeObject<T>((string)o);
        ...
        ...
    }
    
    0 讨论(0)
提交回复
热议问题