ASP.NET MVC transfers strongly typed data to the main page

Duplicate

Transferring data to the home page in ASP.NET MVC

Should the ASP.NET main page receive data from the view?

I ran this method to pass shared data to site.master. However, this requires specific casting of ViewData, and I do not like to use string identifiers everywhere. Is this the best way to do this, or is there another way?

http://www.asp.net/learn/MVC/tutorial-13-cs.aspx

thanks

+6
asp.net-mvc
source share
2 answers

You can create a base class that all your models inherit from:

class MasterModel { // common info, used in master page. } class Page1Model : MasterModel { // page 1 model } 

Then your main page inherits from ViewMasterPage<MasterModel> , and your Page1.aspx inherits from ViewPage<Page1Model> and sets Site.master as the main page.

+8
source share

As I fought a little, here is my contribution:

 class ModelBase { // common info, used in master page. } class Page1Model : ModelBase { // page 1 model } public class ControllerBase : Controller { protected override void OnActionExecuted(ActionExecutedContext filterContext) { var model = ViewData.Model as ModelBase; // set common data here base.OnActionExecuted(filterContext); } } 
+2
source share

All Articles