TypedView to MVC Index controller

Posts   
 
    
nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 23-Jun-2009 01:43:48   

Hi,

I was wondering if anyone had a clue of how to bind a TypedView to the Index view in ASP.NET MVC.

It's quite straight-forward to use an Entity to do this, but I'm not able to figure out the same for a TypedView.

Thanks Arun

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 23-Jun-2009 06:38:52   

What do you mean by "bind"? Are you talking about the controller or the view? If by the view, you should be able to do a "return View(your_typed_list)" from the Index method in the controller and then right click and add a strongly typed view from that method in the controller.

nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 23-Jun-2009 17:36:44   

My apologies for the incomplete question, but you came close to what I want.

Alright, here's what I need to do:

I have a Typedview that I want the controller to load in the index method. When I right click in the Index method and say 'Add View', I get the following options:

ItemMasterPrototype.Data.TypedViewClasses.PrototypeProductLineRow and ItemMasterPrototype.Data.TypedViewClasses.PrototypeProductLineTypedView

(I have added the sql view as a TypedView only.)

Which one do I select?

Here's my controller:


        private readonly PrototypeProductLineTypedView prototypeProductLineTypedView;
        public ProductLinesController()
        {
            prototypeProductLineTypedView = new PrototypeProductLineTypedView();
            prototypeProductLineTypedView.Fill();
        }

        //
        // GET: /ProductLines/
        public ActionResult Index()
        {
            return View(prototypeProductLineTypedView);
        }

If I select the PrototypeProductLineTypedView, I get the fields like Count, CaseSensitive, IsInitialized and so on in the index.aspx page.

If I select the PrototypeProductLineRow, I get the correct fields to be populated in the index.aspx page, but I'm having a type casting issue as I'm passing a PrototypeProductLineTypedView and the view expects as PrototypeProductLineRow.

What am I missing?

Thanks Arun

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 23-Jun-2009 19:10:21   

I think I understand. At the top of your view you should have a page directive that has an "Inherits" attribute. It is there that you put the strongly typed View class you wish to use (although there is no class per-se). You will need to do something like:

Inherits="System.Web.Mvc.ViewPage< System.Collections.IEnumerable< ItemMasterPrototype.Data.TypedViewClasses.PrototypeProductLineRow > >"

If you want to represent a list of them in your view or

Inherits="System.Web.Mvc.ViewPage<ItemMasterPrototype.Data.TypedViewClasses.PrototypeProductLineRow>"

if you want only one to be represented in your view.

Let me know if this helps.

nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 23-Jun-2009 19:47:42   

I'm getting the error:

The model item passed into the dictionary is of type 'ItemMasterPrototype.Data.TypedViewClasses.PrototypeProductLineTypedView' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ItemMasterPrototype.Data.TypedViewClasses.PrototypeProductLineRow]'.


        private readonly PrototypeProductLineTypedView prototypeProductLineTypedView;
        public ProductLinesController()
        {
            prototypeProductLineTypedView = new PrototypeProductLineTypedView();
            prototypeProductLineTypedView.Fill();
        }

        //
        // GET: /ProductLines/
        public ActionResult Index()
        {
            return View(prototypeProductLineTypedView);
        }

How do I convert the TypedView to type PrototypeProductLineRow?

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 24-Jun-2009 04:53:30   

There must be a way to cast it as an IEnumerable. If I remember right, the typed view is simply a strongly typed dataset. Having said that, the instance of the typed-view must have a property that inherits from IEnumerable that is the list of rows. Pass that in to the view. Does that make sense?

nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 24-Jun-2009 19:33:36   

There must be a way to cast it as an IEnumerable.

That is exactly what I'm trying to find out.

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 24-Jun-2009 20:43:35   

Seth wrote:

Having said that, the instance of the typed-view must have a property that inherits from IEnumerable that is the list of rows. Pass that in to the view.

Did you find the property in the typed view that is an IEnumerable of rows?

nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 24-Jun-2009 22:57:59   

No, I have not been able to find the property that is an IEnumerable of rows.

I tried the following but din't work:

    private readonly PrototypeProductLineTypedView prototypeProductLineTypedView;

    public ProductLinesController()
    {
        prototypeProductLineTypedView = new PrototypeProductLineTypedView();
        prototypeProductLineTypedView.Fill();
    }

    //
    // GET: /ProductLines/
    public ActionResult Index()
    {
        // return View(prototypeProductLineTypedView.GetEnumerator());
        // return View(prototypeProductLineTypedView.Rows.GetEnumerator());
    }

Please help, Arun