传递参数列表来查看使用MVC实体框架 [英] Passing parameterized list to view in MVC using Entity Framework

查看:123
本文介绍了传递参数列表来查看使用MVC实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个MVC项目想创建具有从父表的标题和其相关的子表下方的列表随后的视图。

我把使用实体框架的表到项目,并在控制器命名空间创建的follwing视图模型:

 公共类ServicesViewModel
{
    公共ServicesViewModel(列表< ServiceGroup所> servicegroups,列表与LT;服务与GT;服务)
    {
        this.ServiceGroups = servicegroups;
        this.Service =服务;
    }    公开名单< ServiceGroup所> ServiceGroups {搞定;组; }
    公开名单<服务与GT;服务{搞定;组; }}

然后在控制器的ActionResult我这样做:

 公众的ActionResult指数()
    {        变种servicegroups = _db.ServiceGroupSet.ToList();
        VAR的服务= _db.ServiceSet.ToList();        返回查看(新ServicesViewModel(servicegroups,服务));
    }

..并在视图这样做:


  

<%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage%>


  
  

其他HTML code等。


 <%的foreach(在Model.ServiceGroups VAR米){%GT;    <强>< UL> <%= m.ServiceGroupName%GT;< / UL>< / STRONG><%的foreach(在Model.Service VAR项){%GT;    <立GT; <%= item.ServiceDescription%GT;< /李><%}%GT;
<%}%GT;

这所有电线了确定,但运行的每个父记录所有子记录。我还没有想出如何把这些参数,过滤到视图中。

我试过在控制器LINQ查询,但似乎无法找到智能感知的外键字段。


解决方案

嗯..如果它是一个主从2表和你想有一个独立的视图模型,我会建议更换您定义你作为表示ServiceGroup +列表,然后通过这个列表来查看。

但如果你的服务,并表示ServiceGroup确实有自己的导航性能(EF或LINQ2SQL)。
您只需通过ServiceGroups查看和使用有导航属性ServiceGroup.Services列举的细节。

 <%的foreach(在Model.ServiceGroups VAR米){%GT;    <强>< UL> <%= m.ServiceGroupName%GT;< / UL>< / STRONG>**<%的foreach(在m.Services VAR项){%GT; **    <立GT; <%= item.ServiceDescription%GT;< /李><%}%GT;
<%}%GT;

In a MVC project am trying to create a view which has the Title from a parent table and the subsequent list below it from its related Child table.

I pulled the tables into the project using Entity Framework and in the controllers namespace created the follwing Viewmodel:

public class ServicesViewModel
{
    public ServicesViewModel(List<ServiceGroup> servicegroups, List<Service> services)
    {
        this.ServiceGroups = servicegroups;
        this.Service = services;
    }

    public List<ServiceGroup> ServiceGroups { get; set; }
    public List<Service> Service { get; set; }

}

Then in the controller Actionresult I did this:

    public ActionResult Index()
    {

        var servicegroups = _db.ServiceGroupSet.ToList();
        var services = _db.ServiceSet.ToList();

        return View(new ServicesViewModel(servicegroups,services));
    }

..And in the View did this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

Other html code etc..

<% foreach (var m in Model.ServiceGroups) { %>

    <strong><ul> <%= m.ServiceGroupName %></ul></strong>

<% foreach (var item in Model.Service) { %>

    <li> <%= item.ServiceDescription %></li>

<% } %>


<% } %>

It all wires up ok but runs ALL child records for each parent record. I have not figured how to put that parameter which filters into the view.

I tried a linq query in the controller but can't seem to find the foreign key field in the intellisense.

解决方案

Hmmm.. if it is a Master-Detail 2 tables and you want to have a separate view model, I'd recomend that you define you as ServiceGroup + List and then pass list of this to view.

But if Your Service and ServiceGroup do have their own navigation properties (EF or LINQ2SQL). You can just pass ServiceGroups to view and there use Navigation Property ServiceGroup.Services to enumerate details.

<% foreach (var m in Model.ServiceGroups) { %>

    <strong><ul> <%= m.ServiceGroupName %></ul></strong>

**<% foreach (var item in m.Services) { %>**

    <li> <%= item.ServiceDescription %></li>

<% } %>


<% } %>

这篇关于传递参数列表来查看使用MVC实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆