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

查看:545
本文介绍了如何在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页面中返回带有模型的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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