dimanche 19 avril 2015

Passing objects to layout in mvc 5

I'm passing objects to the Layout from the controller like this:



public ActionResult MyProfile() {
var roles = new List<int?>();
User.Roles.ForEach(r => roles.Add(r.ID));
return View(new ProfileModel() { LoginUser = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string.IsNullOrEmpty(User.LastName) ? string.Empty : User.LastName.ToLower()), UserRole = new List<int?>(roles) });
}

public class ModelBase {

[DisplayFormat(ConvertEmptyStringToNull = false)]
public string LoginUser { get; set; }

public List<int?> UserRole { get; set; }

public ModelBase() {
UserRole = new List<int?>();
}
}

public class ProfileModel : ModelBase { }


This works but, I have to do this for all my Controller Action when returning a view.


Is there a way for me to do this just once, without having to repeat it in my actions?


I try adding to it to a base controller, but ROLES and LOGINUSER were always null.


I know this has been addressed a lot on SO, but they are all doing the something.


Thanks.


Aucun commentaire:

Enregistrer un commentaire