简体   繁体   English

如何从控制器获取变量

[英]How to get a variable from controller

How can I access to a variable from my controller and use it in my page? 如何从控制器访问变量并在页面中使用它? This is the code of my controller: 这是我的控制器的代码:

public ActionResult Agence(AgenceQuery model)
{
    var result = Test.Areas.Admin.Models.WebUser.GetUser(model.Num_RA);
    if (result == null)
    {
        ModelState.AddModelError("CustomError", "Cette agence n'éxiste pas");
        return View();
    }

    return View();
}

And thank you everyone for your help. 并感谢大家的帮助。

Use ViewBag 使用ViewBag

In the Controller 在控制器中

public ActionResult Agence(AgenceQuery model)
{
    var result = Test.Areas.Admin.Models.WebUser.GetUser(model.Num_RA);
    if (result == null)
    {
        ViewBag.Error = "CustomError, Cette agence n'éxiste pas";
    }

    return View();
}

in the View, retrieve it : 在视图中,检索它:

@Html.Raw(ViewBag.Error)

Try 尝试

ViewBag.Result = result;

And in your view: 在您看来:

@ViewBag.Result.FieldThatYouWantToDisplay

Or 要么

ViewData["Result"] = result;

And in your view: 在您看来:

@ViewData["Result"].FieldThatYouWantToDisplay

Or 要么

return View(result)

And in your view: 在您看来:

@model Full.Path.To.The.Type.Of.Result.Variable

@Model.FieldThatYouWantToDisplay

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

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