简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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