MVC 5将多个模型放入单个视图中

时间:2015-05-18 21:32:19

标签: asp.net-mvc

MVC的新手,使用MVC 5和VS 2013.
代码首先来自现有数据库,EF6

目标: 想要显示tblModel中所有模型的列表,以获取tblMVP中列出的MVP

以下是项目的当前状态和运行时错误:

错误: System.Collections.Generic.IEnumerable<WebApplication2.Models.tblAccount>&#39;不包含&#39; tblModels&#39;的定义没有扩展方法&#39; tblModels&#39;接受类型System.Collections.Generic.IEnumerable<WebApplication2.Models.tblAccount>的第一个参数可以找到(你是否缺少using指令或汇编引用?)

型号:


    [Table("tblMVP")]
    public partial class tblMVP
    {

        [Key]
        public int ID { get; set; }
        public int AccountID { get; set; }  

        public virtual tblAccount tblAccount { get; set; }
    }
    [Table("tblModel")]
    public partial class tblModel
    {

        [Key]
        public int ID { get; set; }
        public int AccountID { get; set; }  

        public virtual tblAccount tblAccount { get; set; }
    }
    [Table("tblAccount")]
    public partial class tblAccount

    {
    public tblAccount()
        {

            tblModels = new HashSet();
            tblMVPs = new HashSet();

        }

        public int ID { get; set; }    

        public virtual ICollection tblModels { get; set; }
        public virtual ICollection tblMVPs { get; set; }

    }

控制器:

// GET: MVP
public ActionResult Index()
{
var tblAccounts = db.tblAccounts.Include(t => t.tblModels).Include(t => t.tblMVPs);         
return View(db.tblAccounts.ToList());
}

查看

@model IEnumerable<WebApplication2.Models.tblAccount>

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.tblModels.partNumber)
    </th>

    <th>
        @Html.DisplayNameFor(model => model.AccountName)
    </th>

</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.tblModels.partNumber)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.AccountName)
    </td>
    </tr>
}
</table>

2 个答案:

答案 0 :(得分:0)

你正在尝试做两件不同的事情。您有一个IEnumerable<tblAccount>,它没有属性,只有一个tblAccount列表。

所以在顶部,你试图获得IEnumerable的属性,但没有。{/ p>

在底部,您正在迭代IEnumerable,并获取正确执行的tblAccount的属性。

因此,在顶部,您需要从tblAccount获取IEnumerable,以便获取它的属性。以下是如何执行此操作的示例。

@model IEnumerable<WebApplication2.Models.tblAccount>

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.First().tblModels.partNumber)
    </th>

    <th>
        @Html.DisplayNameFor(model => model.First().AccountName)
    </th>

</tr>

答案 1 :(得分:0)

最终,这是最基本的功能版本。无论好坏,创建了tblModel和tblMVP之间的直接关系,并重新安排了控制器中的模型加载。

<强>型号:


    [Table("tblMVP")]
    public partial class tblMVP
    {
        public tblMVP()
        {
            tblModels = new HashSet();
        }

        public int ID { get; set; }

        public int AccountID { get; set; }

        public string Description { get; set; }

        public virtual tblAccount tblAccount { get; set; }

        public virtual ICollection tblModels { get; set; }
    }

    [Table("tblModel")]
    public partial class tblModel
    {        
        [Key]
        public int ModelID { get; set; }

        public int AccountID { get; set; }

        [Required]
        [StringLength(50)]
        public string partNumber { get; set; }        

        [StringLength(5000)]
        public string description { get; set; }

        public int? MVPID { get; set; }

        public virtual tblMVP tblMVP { get; set; }

      }  

    [Table("tblAccount")]
    public partial class tblAccount
    {
        public tblAccount()
        {
           tblModels = new HashSet();
           tblMVPs = new HashSet();            
        }

        public int ID { get; set; }

        [StringLength(150)]
        public string AccountName { get; set; }


        public int? AccountNumber { get; set; }

        public virtual ICollection tblModels { get; set; }

        public virtual ICollection tblMVPs { get; set; }
    }

**Controller:**
    // GET: MVP
    public ActionResult Index()
    {
    var tblModels = db.tblModels.Include(t => t.tblAccount).Include(t => t.tblMVP).ToList();
    return View(tblModels.ToList());
    }

查看

@model IEnumerable<MDLX_Bootstrap_V5.Models.tblModel>

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.tblAccount.AccountName)
    </th>

    <th>
        @Html.DisplayNameFor(model => model.partNumber)
    </th>

</tr>

@foreach (var item in Model)
{        
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.tblAccount.AccountName)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.partNumber)
        </td>
    </tr>
  }
</table>