简体   繁体   中英

All my views are strongly typed, how to pass to site.master?

My views all use strongly typed viewmodels.

Now in my controllers action, I want to set a warning text that will display in my site.master file.

If I set the text in my controllers action, how will it be visible in the site.master?

I'm confused b/c the strongly typed view for the master will be different.

(MVC2)

Also, if I am redirecting to another view, even if I set the viewmodel it will not be used because it is redirect, I know mvc has some sort of a single page session, what is that called again?

For all of my Strongly Typed ViewModels, I use a common base class and interface for this. By default the master page gets them.

public class ViewModelBase : IViewModel {
    public string InformationForMasterPage{get;set;}
}
public class ViewModelHomePage : ViewModelBase{
    public string SomeInformationNotNeededForMasterPage{get;set;}
}
public interface IViewModel{
    public string InformationForMasterPage{get;set;}
}

At the Top of the Home Page:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ViewModelHomePage>" %

Then for the top of the Master Page:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<IViewModel>" %>

And then deep in the master page:

 <%: Model.InformationForMasterPage %>

Will work just fine.

在控制器中,您可以将对象保存在ViewDataViewBag字典中,并从视图和主控中访问它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM