开发者

How to organize partial view in ASP.NET MVC3?

I am trying to figure out how to organize my partial views in my project. Some people say to precede the name of a partial view with an _, but that makes for weirdly named actions in the controller if the view can be called directly.

Also, what should be done if the view can be a partial view in some case开发者_运维百科s and a regular view in other cases?

A common example for this is a search view that I embed on some pages to search for users in my app, but I also have a search page that loads the same view. I suppose I could create a second view for the search page that just embeds the partial view. Just wondering what other people are doing.


Honestly it's a matter of preference. You should do whatever works in your application with respect to avoiding code (or view) duplication etc.

The reason why we (I'm a dev on the team developing MVC) recommend preceding the partial view filename with an underscore is to more easily distinguish between full and partial views when looking at files in VS


I also use my partials with the underscore character as a prefix to easily distinguish between a view and a partial view when managing the files. As your project becomes bigger you may have a lot of files for a single controller, so this convention will help you a lot. Besides, when you use a partial view you can call your views with an action using the following:

public ActionResult MyPartialAsAView()
{
    // your code
    return View("_myPartialView");
}

You have to remember that if you are using your partial as a View, you should assign the layout to it depending on the mode the partial is working (as a view or partial view), for example with a boolean property on your model class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜