开发者

What's the equivalent of ASCX-Code Behind in MVC 3?

开发者 https://www.devze.com 2023-03-22 22:19 出处:网络
I\'ve never built a large project using ASP.NET MVC 3, but I\'m trying to be true to form and use t开发者_Go百科he framework as it was designed, so before I go out on a crazy binge of Html Helpers, I

I've never built a large project using ASP.NET MVC 3, but I'm trying to be true to form and use t开发者_Go百科he framework as it was designed, so before I go out on a crazy binge of Html Helpers, I was wondering what's the best way to create "custom controls" on MVC 3.

Here is a simple example: I might have a dynamically generated Navigation Menu. Menu items would be decided based on where you are on the site, some static menu items, some dynamic menus based on a database, etc.

The two ways I see for me to do that while isolating the NavMenu from the View is to create either a Partial View or an Html Helper.

Partial View seems to be the way to go, since I might have 100s of such little components. However, the Partial View is supposed (according to my understanding) to take care only of the presentation layer and not the Model-logic. At the same time, I don't want the Model of the main view to contain 100 sub-Model for each partial view.

Should I put stuff on the ViewBag? Should I have a Base Controller that takes care of populating the ViewBag / ViewData with whatever I need to render that partial view? Or, the right way is to create HtmlHelpers and use C# to generate all the HTML?

My concerns are maintainability of the code, performance and being able to get designers access to the product to play around with different visual representations of the same content.

In other words, what's the equivalent of ASCX + Code-Behind in MVC 3?


You should make child actions in a separate Menu controller that gather the data into models and return partial views.
You can use the same model for different actions.

0

精彩评论

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

关注公众号