如何在ASP.Net Core Razor页面中返回带有模型的页面

编程入门 行业动态 更新时间:2024-10-26 20:30:57
本文介绍了如何在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页面中返回带有模型的页面

本文发布于:2023-11-24 19:00:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   模型   如何在   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!