开发者

Using LLBL as Model in MVC

I have settled on trying to use ASP.NET MVC but the first part I want to replace is the Model. I am using LLBL Pro for the model.

I have a table called "Groups" that is a simple look up table. I want to take thhe results of the table and populate a list in MVC. Something that should be very simple... or so I thought.... I've tried all kinds of things as I was gett开发者_JAVA技巧ing errors like:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[glossary.EntityClasses.GroupEntity]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[glossary.CollectionClasses.GroupCollection]'.

private GroupCollection gc = new GroupCollection();

    public ActionResult Index()
    {

      gc.GetMulti(null);
      return View( gc.?????? );
    }

This is all I am trying to do, I've tried lots of variations, but my goal is simply to take the data and display it.


Not sure if this would work, but you could try wrapping the EntityCollection into a ViewModel class and passing it to the View like so:

public class GroupsViewModel()
{
    public GroupCollection Groups { get; set; }
    // other items in your view model could go here
}

then convert your controller method to

public ActionResult Index()
{
    GroupCollection gc = new GroupCollection();
    gc.GetMulti(null);
    GroupsViewModel vm = new GroupsViewModel();
    vm.Groups = gc;
    return View(vm);
}

I like this approach because each ViewModel is an object in-and-of itself.


You can use the AsEnumerable extension where your ????? are or change the type of your ViewUserControl(in the markup) to be of type System.Collections.Generic.List. Basically what you need to correct is the mismatch between the type of the View and the Model being passed in.


I'm not sure about your exact error, but I'd venture a guess that one of two things are happenging:

  • You are making some sort of invalid / illegal call on your LLBLGen object. If this is the case make sure you are setting it up right / calling right method / property etc.

  • The model you are passing to the veiw is too hairy for it to deal with. In this case, and in general, you should create a light 'View Model' class with just the data you want displayed and populate it from your LLBLGen object first then pass it to the view, which will be able to easily handle your view model class.

Here are some references:

  • http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx
  • http://nerddinnerbook.s3.amazonaws.com/Part6.htm
  • http://www.codinginstinct.com/2008/10/view-model-inheritance.html


Stemming off what Yuriy said, it looks like your view is strongly typed to a "collection" of a collection of your groupentity, and you are trying to pass just the collection of your groupentities. Make sure your "collection" type (IEnumerable, IList, etc) matches what type of collection you are sending in your controller, along with the type of the actual object in the collection.

View: System.Collections.Generic.List1[glossary.EntityClasses.GroupEntity]

Controller: System.Collections.Generic.List1[glossary.EntityClasses.GroupEntity]

Just a thought

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜