Create a base class for your models with the UserName property:
public abstract class ModelBase { public string UserName { get; set; } }
Create a base class for your controllers and override it with the OnActionExecuted method. Inside it, check if the model from BaseModel is looped, and if so, set its UserName property.
public class ControllerBase : Controller { protected override void OnActionExecuted( ActionExecutedContext filterContext) { var modelBase = ViewData.Model as ModelBase; if (modelBase != null) { modelBase.UserName = "foo"; } base.OnActionExecuted(filterContext); } }
You can then display the user name in the view as follows:
<%= Html.Encode(Model.UserName) %>
See also :
- Best Practices, Tips, and Tricks for ASP.NET MVC
Konstantin tarkus
source share