MVC 在单个视图中处理多个 LINQ 查询

发布于 2024-12-11 19:15:29 字数 379 浏览 0 评论 0原文

现在,我意识到这个问题之前已经得到了回答,但我就是无法思考它并使其在我的示例中发挥作用:

View
应包含 2 个 foreach 循环 fx
foreach(Model.ListA 中的 var item)
foreach(var item in Model.ListB)

Model
应包含一个具有 LinqDataContext 对象和两个属性的类:(ListA 和 ListB)

Controller
应通过通过视图的模型。


模型和控制器如何实现这一目标? 一些简单的代码示例会非常有帮助:)

Now, I realize that this question has been answered before, but I just can't warp my head around it and make it work in my example:

View
should contain 2 foreach loops fx
foreach(var item in Model.ListA)
foreach(var item in Model.ListB)

Model
should contain a class with a LinqDataContext object and two properties: (ListA and ListB)

Controller
should pass the Model through the View.

How would the Model and Controller look to achieve this?
Some simple code examples would be really helpful :)

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

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

发布评论

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

评论(3

山人契 2024-12-18 19:15:29

你这有点落后了。您的数据上下文应该位于控制器中(最好位于控制器使用的更低层中)。您始终希望控制器负责获取/更新数据并填充模型。然后,模型将被传递到视图,其中包含呈现该数据所需的一切。

public class MyModel
{
    public List<ListAEntity> ListA {get;set;}
    public List<ListBEntity> ListB {get;set;}
}


public class HomeController : Controller
{
    private readonly MyDataContext _context = new MyDataContext();

    public ActionResult Index()
    {
        var model = new MyModel()
        {
            ListA = _context.Get<ListAEntity>().ToList(),
            ListB = _context.Get<ListBEntity>().ToList()
        };

        return View(model);
    }
}

You've got it a bit backward. Your data context should be in the controller (preferably in a layer even lower that the controller uses). You always want your controller responsible for getting/updating data, and populating the model. Then, the model is delivered to the view with everything needed for the presentation of that data.

public class MyModel
{
    public List<ListAEntity> ListA {get;set;}
    public List<ListBEntity> ListB {get;set;}
}


public class HomeController : Controller
{
    private readonly MyDataContext _context = new MyDataContext();

    public ActionResult Index()
    {
        var model = new MyModel()
        {
            ListA = _context.Get<ListAEntity>().ToList(),
            ListB = _context.Get<ListBEntity>().ToList()
        };

        return View(model);
    }
}
多彩岁月 2024-12-18 19:15:29

哇哦,将 LinqDataContext 填充到 View 的味道非常糟糕。你为什么要这么做?

控制器应该从所述 LinqDataContext 或后端服务获取所需的所有数据,然后创建一个仅包含 IList 或 IEnumerable 的简单 ViewModel。

public class YourViewModel 
{
    public List<A> ListA {get; set;}
    public List<B> ListB {get; set;}
}

public ActionResult YourControllerAction()
{
   var context = yourDataContext;

   var model = new YourViewModel
   {
      ListA = context.TableA.Where(x => x.Something)
                     .Select(x => x.ConvertSqlToBusinessObject()).ToList(),
      ListB = context.TableB.Where(x => x.Something)
                     .Select(x => x.ConvertSqlToBusinessObject()).ToList()
   };

   return View("Index",model);
}

Whoa, padding a LinqDataContext to a View smells pretty bad. Why would you do that?

The controller should get all the data that it needs either from said LinqDataContext or from a backend service and then create a simple ViewModel that only contains an IList or IEnumerable.

public class YourViewModel 
{
    public List<A> ListA {get; set;}
    public List<B> ListB {get; set;}
}

public ActionResult YourControllerAction()
{
   var context = yourDataContext;

   var model = new YourViewModel
   {
      ListA = context.TableA.Where(x => x.Something)
                     .Select(x => x.ConvertSqlToBusinessObject()).ToList(),
      ListB = context.TableB.Where(x => x.Something)
                     .Select(x => x.ConvertSqlToBusinessObject()).ToList()
   };

   return View("Index",model);
}
朱染 2024-12-18 19:15:29

我想在前面的答案中添加一个小补充:控制器应该实现选择正确的视图和视图模型所需的逻辑,但它不应该填充视图模型。视图模型完全能够填充自身。

这种模式改进了控制器和视图模型的封装,并在关注点之间创建了更清晰的界限。因此,如果我窃取迈克尔的代码片段:

在控制器中

public ActionResult YourControllerAction(){
   MyDbContext context = new MyDbContext();
   return View("YourControllerAction", new YourViewModel(context));
}

在视图模型中

public class YourControllerAction {
    public YourControllerAction(MyDbContext context) {
         ListA = context.TableA.Where(x => x.Something).Select(x => x.ConvertSqlToBusinessObject()).ToList();
         ListB = context.TableB.Where(x => x.Something).Select(x => x.ConvertSqlToBusinessObject()).ToList();
    }
}

I would add a small addition to the previous answers: The controller should implement the logic necessary to select the proper view and view model, however it should NOT populate the View Model. The view model is perfectly capable of populating itself.

This pattern improves the encapsulation of both the controller and view model as well as creating a cleaner demarcation between concerns. Thus, if I steal Michael's code snippet:

In the controller

public ActionResult YourControllerAction(){
   MyDbContext context = new MyDbContext();
   return View("YourControllerAction", new YourViewModel(context));
}

In the view model

public class YourControllerAction {
    public YourControllerAction(MyDbContext context) {
         ListA = context.TableA.Where(x => x.Something).Select(x => x.ConvertSqlToBusinessObject()).ToList();
         ListB = context.TableB.Where(x => x.Something).Select(x => x.ConvertSqlToBusinessObject()).ToList();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文