开发者

Should a model contain all of the data required to build the view?

Lets say I have a site that has a Layout with navigation. This navigation is built each time a page is requested based on options the user has chosen. This navigation does not really have anything to do with the particular view being pres开发者_高级运维ented except that it is required for the Layout. The question is, should this be put in the ViewData or should the Model be required to inherit a NavigationModel?

ViewBag.NavItems = navItems;

or

public abstract class NavigationModel
{
    public List<NavItem> NavItems { get; set;}
}

public class HomeModel : NavigationModel
{
}


This sounds like an ideal case to use Html.RenderAction inside of the view to render the navigation. This would call an action on a NavigationController that would be responsible for rendering the menu directly into the view.

Some MVC purists feel that Html.RenderAction breaks true MVC architecture; I see it as a way of delegating responsibility to render certain parts of the view that are not the primary responsibility of the "main" view that you're rendering.


Should a model contain all of the data required to build the view?

Absolutely and definitely YES.

The question is, should this be put in the ViewData

Absolutely and definitely NO.

You also have the possibility of using Html.Action and Html.RenderAction if this navigation widget could be considered as fully independent part of your site.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜