0

I have a requirement like on my layout header I have to show the LocationName, UserRole and UserName apart from that we have some common properties which will gonna use internally to perform some logics based on the request.

To accomplish this I have created one baseModel

 public class BaseModel
    {
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string RoleName { get; set; }
        public string RoleId { get; set; }
        public int UserId { get; set; }
        public Dictionary<int,string> Orders { get; set; }
        public Dictionary<int,string> CustomersBirthday { get; set; }
        public int BranchId { get; set; }
        public SelectList Branch { get; set; }
    }

Scenario 1:

I wanted to fill this BaseModel with values at the time of successfull login, So that In each request the values should persist and I can use the same values.

Scenario 2: Whenever we do have any hit from client side for any controller at that time the values for BasedModel should not washed away.

The fact is I don't want to use the TempData and ViewBag. Therefore I am Looking for some alternative solution like this "BaseModel" class.

Any suggestion would be highly appreciable!!!

vijay sahu
  • 765
  • 1
  • 7
  • 29
  • Use a base model http://stackoverflow.com/questions/13225315/pass-data-to-layout-that-are-common-to-all-pages or create actions that return a partial in layout.cshtml using @Html.Action("myAction", "myController") – Animus Miles-Militis Mar 27 '17 at 10:46
  • You should consider this information storing in session or in custom Identity object and populate it to the base model while rendering the view. And use it to query the database or storing it to the database. Binding it to view and submitting everyting from the view will not be a good decision in long run. – Chetan Mar 27 '17 at 10:48

2 Answers2

0

You have to add BaseModel to session.

So you are sure every user will get only one.

To set data you use this code:

Session["baseModel"] = baseModel;

to get data, use this one:

var baseModel = Session["baseModel"] as BaseModel;

Ygalbel
  • 5,214
  • 1
  • 24
  • 32
  • Your suggestion is appreciable. But for loggedin user I am already using session and in this on each request every time I have to write the same code to fetch the value from session. I need a generic way to fetch this value.. like onExecuting() method , I will get all these values for BaseModel and then this base model will automatically assigned to all other child model where I am redirecting. – vijay sahu Mar 27 '17 at 11:47
  • Why you don't use inheritance for controllers. So BaseModel is in parent, and exist in every controller. – Ygalbel Mar 27 '17 at 11:56
  • That is an absolutely *awful* idea. Do not, for the love all things good and holy in the world, make every controller inherit from a base controller for the sole purpose of passing some particular model. – Chris Pratt Mar 27 '17 at 16:19
0

Your question is very difficult to understand, but if I'm getting the gist, you have some bit of information you want rendered on every page, probably as part of your layout.

For that, you should use a child action. In whatever controller you like (you'll specify which explicitly, so it doesn't matter), add something like the following:

[ChildActionOnly]
public ActionResult UserInfoHeader()
{
    // fetch or otherwise build your user info model
    return PartialView("_UserInfoHeader", model);
}

Create _UserInfoHeader.cshtml:

@model Namespace.To.BaseModel

<!-- render user info here -->

Then, in your layout, add the following where you want this HTML to appear:

@Html.Action("UserInfoHeader", "Foo")

Where "Foo" is the controller you put this action in.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444