English 中文(简体)
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 hierarchical 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.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签