Passing a value from a controller to a general view in MVC

I need to send some values ​​from the controller to the general view to show at the top

[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 do not want to use a TempData, viewdata, viewbag or session, as I can send it using querystring or adding to the model.

This is one of the layouts:

  <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> 
+5
source share
5 answers

I assume that you want this for the current request, and not for all requests, so the accepted answer is not correct. To exchange data with views or child controllers as part of a single request, the easiest way is to put your data in HttpContext.Items. This is used by all views and child controllers during the same request.

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

You can abstract this with the 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 the parameters

 HttpContext.GetUIOptions().ShowMenu= true 

In your view, refer to it as follows:

 ViewContext.HttpContext.GetUIOptions() 

I usually describe this further so that you customize it with attributes like

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

So, you write an ActionFilter that checks the attributes of the action and sets the properties of the httpContext.GetUIOptions () object using the attribute properties during the ActionExecuting phase.

+1
source

Create one common / common class and define one public static property there and set its value from the controller and access it, as shown below:

GeneralClass:

 public static class GeneralUtility { /// <summary> /// Username /// </summary> public static string Username = ""; } 

Controller:

 GeneralUtility.Username ="sandip"; 

View:

 @if(GeneralUtility.Username=="sandip") { // rest of the logic } 
+1
source

In your view, you can read the value from the query string:

 @Request.QueryString["name"] 

Then it's just a matter of including the value you want in the querystring, which can be easily processed when you do a RedirectToAction, for example:

 return RedirectToAction("Details", new { id = id, name = "value" }) 
+1
source

In this case, Session more applicable than a static field or property in a static class. you can add an extension method to HttpContext.User.Indetity to display the username.

 public static class UserExtension { public static void SetDisplayName(this IIdentity identity, string fullName) { if(HttpContext.Current.User.Identity.IsAuthenticated) HttpContext.Current.Items[HttpContext.Current.User.Identity.Name] = fullName; } public static string GetDisplayName(this IIdentity identity) { return HttpContext.Current.Items[HttpContext.Current.User.Identity.Name] as string; } } 

in the controller:

 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) { User.Identity.SetDisplayName(user.Name); 

and in shared mode:

 <div class="primary-nav"> <ul> <li>User.Identity.GetDisplayName()</li> 
0
source

You need to pass the general model to your layout. Let them say SharedModel Definition of the model would be

 public class SharedModel { public string Name {get; set;} public bool IsLoggedIn {get; set;} } 

All other models must be inherited from SharedModel. For example, AccountModel

 public class AccountModel : SharedModel { public string Password {get; set;} public string Username {get; set;} } 

Therefore, when you pass a specific model to your view, the overall model will also be passed to your overall layout.

  [HttpPost] [Route("login")] public async Task<ActionResult> Login(LogInRequest logInRequest) { AccountModel aM = new AccountModel(); aM.Name = "Murtaza"; aM.IsLoggedIn = false; .... .... return View(aM): } 
0
source

Source: https://habr.com/ru/post/1258179/


All Articles