如何在 ASP.Net Core Razor 页面中使用模型返回页面 [英] How do return Page with a model in ASP.Net Core Razor Page

查看:32
本文介绍了如何在 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 });

注意:我要返回的Page后面没有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 页面中使用模型返回页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆