简体   繁体   English

如何在 controller 中使用来自 razor 视图的变量

[英]How to use variable from razor view in controller

I have variable in my layout.cshtml and I would like to use it in my controller. Is this possible and how to accomplish this?我的 layout.cshtml 中有变量,我想在我的 controller 中使用它。这可能吗?如何实现?

Although Model Binding is one way in Razor Views but using the hack on TempData , you accomplish this.尽管 Model Binding 是TempData Views 中的一种方式,但使用 TempData 上的 hack,您可以完成此操作。

public ActionResult Foo()
{
    SomeModel model = ...
    TempData["foo"] = model;
    return RedirectToAction("Bar");
}

public ActionResult Bar()
{
    var model = TempData["foo"] as SomeModel;
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM