在 MVC 中使用 LLBL 作为模型

发布于 2024-08-07 23:45:48 字数 661 浏览 3 评论 0 原文

我已决定尝试使用 ASP.NET MVC,但我想要替换的第一部分是模型。我使用 LLBL Pro 作为模型。

我有一个名为“组”的表,它是一个简单的查找表。我想获取表的结果并填充 MVC 中的列表。应该非常简单的事情......或者我认为......我已经尝试了各种各样的事情,因为我收到了错误,例如:

传递到字典中的模型项的类型为“System.Collections.Generic.List” 1[glossary.EntityClasses.GroupEntity]',但此字典需要类型为“System.Collections.Generic.IEnumerable1[glossary.CollectionClasses.GroupCollection]”的模型项。

<代码> private GroupCollection gc = new GroupCollection();

    public ActionResult Index()
    {

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

这就是我想要做的,我尝试了很多变体,但我的目标只是获取数据并显示它。

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 getting 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染柒℉ 2024-08-14 23:45:48

不确定这是否有效,但您可以尝试将 EntityCollection 包装到 ViewModel 类中并将其传递给 View,如下所示:

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

然后将控制器方法转换为

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

我喜欢这种方法,因为每个 ViewModel 本身就是一个对象。

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.

计㈡愣 2024-08-14 23:45:48

您可以使用 AsEnumerable 扩展,您的 ??????将 ViewUserControl(在标记中)的类型更改为 System.Collections.Generic.List 类型。基本上,您需要纠正的是传入的视图类型和模型之间的不匹配。

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.

时光是把杀猪刀 2024-08-14 23:45:48

我不确定您的确切错误,但我大胆猜测发生了以下两件事之一:

  • 您正在对 LLBLGen 对象进行某种无效/非法调用。如果是这种情况,请确保您正确设置/调用正确的方法/属性等。

  • 您传递给 veiw 的模型太复杂,无法处理。在这种情况下,一般来说,您应该创建一个轻量级“视图模型”类,其中仅包含您想要显示的数据,并首先从 LLBLGen 对象填充它,然后将其传递给视图,这将是能够轻松处理您的视图模型类。

以下是一些参考:

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:

羁拥 2024-08-14 23:45:48

根据 Yuriy 的说法,您的视图似乎被强类型化为您的团体实体集合的“集合”,并且您试图仅传递您的团体实体的集合。确保您的“集合”类型(IEnumerable、IList 等)与您在控制器中发送的集合类型以及集合中实际对象的类型相匹配。

看法:
System.Collections.Generic.List1[glossary.EntityClasses.GroupEntity]

控制器:
System.Collections.Generic.List1[glossary.EntityClasses.GroupEntity]

只是一个想法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文