views:

26

answers:

2

I'm trying to design a solution in MVC in which a string representation of a class is passed to the controller which should then build a grid with all the data belonging to that class in the DB. (I'm using an ORM to map classes to tables).

        //A method in the Model that populates the Item Property
    foreach (MethodInfo method in sDRMethods)
    {
        if (method.Name.Contains(_domainTable))
        {
            Items = method.Invoke(repositoryObject, null);
            break;
        }
    }

//View uses this Items property of the Model to populate the grid.
public object Items;

//_domainTable is the name of the table/class (in string format).
//repositoryObject is the object that has mehtods to return IEnumerable<class> collection object of each type.

The problem I have is that I do not know how to cast the "Items" proprety in my view to iterate through it and build a grid.

I have tried using the "http://mvcsharp.wordpress.com/2010/02/11/building-a-data-grid-in-asp-net-mvc/" but the generic extension method is expecting to know the specific type that it should work with.

I would prefer to use MVC but it looks like I cannot easily have this working(which is very hard to believe).

A: 

I really don't like the sound of what you are trying to do. Why convert the table to a string?

The only time you would convert to a string, is when the view gets rendered. And that, in most cases, should be left to the MVC framework.

The code you mentioned uses an HtmlTextWriter which is fine, because it will render straight to the response.

However, it sounds as if you are trying to reinvent the wheel by rendering everything to a string, rather than leaving that to the framework.

Note that in MVC the views are just templates for rendering strings, which is, if I have understood you, exactly what you need.

So, if I have remotely understood what you are trying to do, and it is a big if because your post is not clear, you should pass your class to view as part of the strongly typed model, and then write some basic design logic into the view.

If I am right, which is not certain, I think you have misunderstood how MVC works.

Have a look at a few examples of how to use views to render the data in a model. The model can be any class, it can be an IEnumerable, a list, whatever, and you can use foreach loops in the view to render out what you want, how you want it.

In this sense, MVC is very different to writing custom controls in plain vanilla ASP.NET.

awrigley
A: 

Thanks for your reply awrigley.

The requirement is quite simple. I perhaps made it sound awfully complex in my post.

On an Index view, I have to populate a dropdownlist with all the tables of the application that are system lookup. The "Admin" of the app, selects an item from the dropdownlist which should show the contects of that table in a grid so that the admin can perform CRUD operations using that grid.

What I am trying to do is, pass the selected item (which is the name of the table) to the controller which in turn passes it to the ViewModel class. This class uses reflection to invoke (code shown in my original question) the right method of a repository which has got methods like:

public IEnumerable GetAllTable1Data() { ..... }

The problem I have is that when I invoke the method, it returns a type "object" which I cannot cast to anything specific because I don't know the specific type that it should be cast to. When this object is passed to the view, the grid is expecting an IEnumerable or IEnumerable but I do not know this information. I am not able to do this:

(IEnumerable)method.Invoke(repositoryObject, null)

I get: cannot cast IEnumerable to IEnumerable

I (kind of) have the grid now displaying but I am using a Switch statement in the view that goes:

Switch(SLU_Type) { case "SLU_Table1": Html.Grid((IEnumerable)Model.Items); case "SLU_Table2": Html.Grid((IEnumerable)Model.Items); ..... }

I don't like this at all, it feels wrong but I just cannot find a decent way!

I could have partial views for each of the system look up tables but for that I'll have to add around 30 partial views with almost exactly same code for the Action & View. This does not seem right either!

Hopefully, this gives you a better understanding of what I'm trying to achieve.

amyth