本文介绍了如何在ASP.Net Core Razor页面中返回带有模型的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何重定向到页面并传递其模型? 就像MVC中的功能一样 return View(model:MyModel);
How to I Redirect to a Page and Passing its model? Just like what we have in MVC return View(model: MyModel);
我尝试过的操作: return RedirectToPage( / Notify,new {Model = notifierVM});
注意:我要返回的页面后面没有PageModel
推荐答案MVC内置了字典对象 TempData 。 您可以序列化模型,将JSON字符串放入TempData中,然后在重定向操作上可以将JSON字符串反序列化为对象。
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 }); }在其他操作上
public ActionResult Details(int id) { object o; TempData.TryGetValue("data", out o); var MyModel = JsonConvert.DeserializeObject<T>((string)o); ... ... }更多推荐
如何在ASP.Net Core Razor页面中返回带有模型的页面
发布评论