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

拥有回忆 提交于 2019-12-25 18:49:14

问题


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 RedirectToPage("/Notify", new { Model = notifierVM });

Note: the Page I want to return has no PageModel behind


回答1:


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);
    ...
    ...
}


来源:https://stackoverflow.com/questions/59207419/how-do-return-page-with-a-model-in-asp-net-core-razor-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!