开发者

HTML markup & ASP.NET MVC3

开发者 https://www.devze.com 2023-04-12 02:29 出处:网络
I have parts of the web page which I would like MVC3 to serve to client pages. Instead of making them static in everysite. I have sites which have reoccurring markup in parts in the page, such as bann

I have parts of the web page which I would like MVC3 to serve to client pages. Instead of making them static in everysite. I have sites which have reoccurring markup in parts in the page, such as banner, navigation. In some sites I will need to customize the markup a little. So I need to be able to extend if possible.

Can someone please tell me if MVC3 is ideal as a solution to this? I am thinking partial views. Can you inherit & extend partial views?

Th开发者_JS百科anks


Partial Views can be implemented for your needs. For example, you can use them as follows :

  @model string[]

  @if((bool)Model[0]) { 

    <span>I like it to be this way.</span>

  } else { 

      <span>No, I like it to be that way.</span>
  }

Inside a view, you can call this partial view as follows :

@Html.Partial("myPartialView", new string[] { "true" })

You can pretty much pass anything to partial view as Model.

If you plan to use those partial views on multiple applications, I encourage you to create Nuget package for those Partial Views and get them into each application through Nuget Package Manager.


Banners, navigation, custom panels etc - all these are the components that you might want to add, combine, remove or update as time goes. Yes, partial views would do the job easily, but in my opinion, inheritance isn't a way forward.

Your partial views should be based on view models. For example: OrderPanelViewModel, UserMenuViewModel, CustomPanelViewModel. Each partial view is now a component, a feature that you can add,remove, modify as you feel suited.

What happens if you want to combine banner and a user menu? It's not going to work if they inherit from the same view model, but it will work if they have their own view models.

For example:

public class MyCustomPaneViewModel{
   public UserMenuViewModel UserMenu { get; set; }
   public UserBannerViewModel UserBanner { get; set; }
}

You can now base your view on this view model

@model YourDll.WebUI.Models.MyCustomPaneViewModel

@section main_content{

   @Html.DisplayFor(model => model.UserMenu, "UserMenu")
   @Html.DisplayFor(model => model.UserBanner, "UserBanner")

}
0

精彩评论

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

关注公众号