Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transfer value from Controller to Shared View in MVC

I need to send some values from controller to shared view to show in the top of

    [HttpPost]
    [Route("login")]
    public async Task<ActionResult> Login(LogInRequest logInRequest)
    {
        IEnumerable<UserClaim> UserClaims = null;
        User user = null;
        if (ModelState.IsValid)
        {
      user = await GetUserByEmailAndPassword(logInRequest.UserName, logInRequest.Password);
            if (user.Id != 0)
            {
                 showMenu = await ShowLoanMenu(logInRequest);
                if (showMenu)
                {
        ******** I need to send showMenu and user.Name to shared view
             return RedirectToAction(Constants.Views.SearchView, Constants.Views.LoanDriverController);
                }
            }
               .....
              return View(logInRequest);
    }

I don't want to use TempData, viewdata, viewbag or session, how I can send it by querystring or adding to model.

This is one of the layout:

      <ul>
            <li class="logo">
                <img src="~/Content/Images/logo.png" alt="" />
            </li>
            <li class="nav-item">
                  ***  @if(showmenu is true)
                 {
                    <ul>
                        @Html.ActionLink("Loan Driver", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                    </ul>
                }
            </li>
        </ul>

and this is another layout:

 <div class="header">
       <div class="master-container">
        <div class="heading">Garage</div>
        <div class="primary-nav">
            <ul>
                <li>******show name of the person</li>
                <li>@Html.ActionLink("Logout", "logout", "Home")</li>
               </ul>
           </div>
        </div>
like image 921
Alma Avatar asked Oct 29 '22 18:10

Alma


1 Answers

I guess you want this for the current request, not for all requests so accepted answer is not the correct way. To share data with views or child controlers within the scope of a single request, easiest way is to put your data to HttpContext.Items. This is shared by all views and child controllers during the same request.

HttpContext.Items["UIOptions"] = new UIOptions { ShowMenu = true }; 

You can abstract this with an extension:

public static class HttpContextExtensions
    {
        public static UIOptions GetUIOptions(this HttpContext httpContext)
        {
            var options = httpContext.Items["UIOptions"] ?? (object) new UIOptions();
            httpContext.Items["UIOptions"] = options;
            return options;
        }
    }

Then in your controller, set options

HttpContext.GetUIOptions().ShowMenu= true

In your view, access it like this:

ViewContext.HttpContext.GetUIOptions()

I usually abstract this further such that you configure it with attributes like

[UIOptions(ShowMenu=true)]
public ActionResult MyAction()
{
    return View();
}

So you write a ActionFilter which checks the attributes on the action and sets the httpContext.GetUIOptions() object's properties using the attribute properties during ActionExecuting phase.

like image 153
Cagatay Kalan Avatar answered Dec 20 '22 07:12

Cagatay Kalan