开发者

populating partial views

I have a MVC3 view which is being popula开发者_Python百科ted from controller using view model. Viewmodel is inistialized/populated using a populate method in repository. Now I want to create partial views and want to hide few sections. Do I need to create different methods to populate the view model and I need to break the viewmodela s well ? Please suggest


You could have your view models different complex properties which themselves represent other view models which will be bound to the respective sections. You also might include boolean properties indicating whether the sections need to be visible or not. Example:

@if (Model.ShouldShowFooSection)
{
    @Html.DisplayFor(x => x.Foo)
}

or:

@if (Model.ShouldShowFooSection)
{
    @Html.Partial("_Foo", Model.Foo)
}


There nothing that says you have to define a new viewmodel or repository method in order to support partial views. You'll have to decide if this it's worth it or not based on the details of your scenario.

It may be better for long term maintenance to break things up but that's purely a design choice for you to make.

Depending on how your repository works, there may also be a performance improvement if you only have to retrieve a subset of data - but this could be outweighed by the performance penalty of making multiple repository calls to render a single page.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜