开发者

ASP.Net MVC: Showing the same data using different layouts

I'm wanting to create a page that allows the users to select how they would like to view their data - i.e. summary (which supports grouping), grid (which supports grouping), table (which supports grouping), map, time line, xml, json etc.

Now each layout would probably have different use a different view model, which inherit from a common base class/view model. The reason being that each layout needs the object structure that it deals with to be different (some need hierarchic开发者_JAVA百科al others a flatter structure).

Each layout would call the same repository method and each layout would support the same functionality, i.e. searching and filtering (hence these controls would be shared between layouts). The main exception to this would be sorting which only grid and table views would need to support.

Now my question is given this what do people think is the best approach.

  • Using DisplayFor to handle the rendering of the different types?
  • Also how do I work this with the actions... I would imagine that I would use the one action, and pass in the layout types, but then how does this support the grouping required for the summary, grid and table views.
  • Do i treat each grouping as just a layout type
  • Also how would this work from a URL point of view - what do people think is the template to support this layout functionality

Cheers Anthony


Conceptually, the View is the part of an MVC web app that is responsible for handling the displaying of data. So, if you want to display data in different ways, it seems most logical that each "display" has its own corresponding aspx View.

All of the View Models can inherit from the same base model. So, for example, we might have four models and three views:

public abstract class BaseViewModel {}
public class GridViewModel : BaseViewModel {}
public class TableViewModel : BaseViewModel {}
public class SummaryViewModel : BaseViewModel {}

GridViewPage<GridViewModel>
TableViewPage<TableViewModel>
SummaryViewPage<SummaryViewModel>

Each of the views can have different stylehsheets and javascript files attached, so you should be able to use DisplayFor if you'd like, or you can create the layout by hand.

As for the controller, you can create one action method that returns any of the three views, or you could create three separate ActionResults, one for each view. Here's the "monolithic" ActionResult:

public ActionResult PageViewResult(string pageType)
{
    switch (pageType)
    {
        //define your cases, return your views and your models
        //make sure to set a default
    }
}

You can format the routes however you see fit. For example, with the above "monolithic" ActionResult, we could create the following route in our Global.asax file:

    routes.MapRoute(
        "FormattedViewPage", // Route name
        "View/Page/{pageType}", // URL with parameters
        new { controller = "ViewPageController", action = "PageViewResult", pageType = "grid" } // Parameter defaults
    );

Hope this helps. Let me know if you have any questions.


First of if it's the same data then I would try to use the same model for all the views but either use different css or different aspx pages/controls for rendering the model depending on how the user wants to view the data.

Also you may want to look into how much of this can be done in JavaScript, it all depends on how rich you want the user experience to be.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜