在IEnumerable视图中访问两个模型 [英] access two models in a IEnumerable view

查看:67
本文介绍了在IEnumerable视图中访问两个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个视图中访问两个模型,基本上我有两个模型

I'm trying to access a two models in one view, basically i have two models

用户模型

namespace SLMDemo0.Models
{
    using System;
    using System.Collections.Generic;

    public partial class User
    {
        public int UID { get; set; }
        public string UserName { get; set; }
        public int PID { get; set; }
        public int LID { get; set; }
        public string SK { get; set; }
        public string Brand { get; set; }
        public string CN { get; set; }
        public string SN { get; set; }
        public string AT { get; set; }
        public string Ref { get; set; }
        public Nullable<System.DateTime> DC { get; set; }

        public virtual License License { get; set; }
        public virtual License License1 { get; set; }
    }
}

产品型号

namespace SLMDemo0.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Product
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Product()
        {
            this.Licenses = new HashSet<License>();
        }

        public int PID { get; set; }
        public int VenID { get; set; }
        public string PName { get; set; }

        public virtual Vendor Vendor { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<License> Licenses { get; set; }
    }
}

我在用户控制器中有Details Action

And I have Details Action in User Controller

public ActionResult Details(int S)
    {
        SLMEntitiesDB dbContext = new SLMEntitiesDB();
        var VL = (from U in dbContext.Users
                  join P in dbContext.Products
                  on U.PID equals P.PID
                  where P.PID == U.PID
                  select new UP()
                  {
                      UserO = U,
                      ProductO = P
                  }).Where(U => U.UserO.LID == S).ToList();

        return View(VL);
    }




     @model SLMDemo0.Models.UP

 @{
     ViewBag.Title = "Details";
 }

 <h2>License Details</h2>
 <p>
     @using (Html.BeginForm("Details", "Users", FormMethod.Get))
     {
         <b>Search By:</b>
         @Html.RadioButton("searchBy", "username") <text>Username</text>
         @Html.TextBox("Search") <input type="submit" value="Search" />
     }
 </p>

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

         <th>
             @Html.DisplayNameFor(model => model.ProductO.PName)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.UserO.SK)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.UserO.Brand)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.UserO.CN)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.UserO.SN)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.UserO.AT)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.UserO.DC)
         </th>
         <th>
             @Html.DisplayNameFor(model => model.UserO.Ref)
         </th>
         <th></th>
     </tr>

     @foreach (var item in Model.ProductO)// issue is here 
     {
 <tr>
     <td>
         @Html.DisplayFor(modelItem => item.UserName)
     </td>


     <td>
         @Html.DisplayFor(modelItem => item.SK)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.Brand)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.CN)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.SN)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.AT)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.Ref)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.DC)
     </td>
     <td>
         @Html.ActionLink("Edit", "Edit", new { id = item.UID }) |
         @Html.ActionLink("Details", "Details", new { id = item.UID }) |
         @Html.ActionLink("Delete", "DelUL", new { L = item.UID }, new { 
onclick = "return confirm('Are sure you want to permanently
 delete this license ?');" })
     </td>
 </tr>
     }

现在在详细信息视图中,foreach出现问题,即foreach语句无法对 Product 类型的变量进行操作,因为产品不包含GetEnumerator'的公共实例定义.

Now in details view, foreach has issue saying foreach statement cannot operate on variable of type Product because product does not contain a public instance definition for GetEnumerator'.

推荐答案

您的查询应如下所示:

public ActionResult Details(int S)
{
    SLMEntitiesDB dbContext = new SLMEntitiesDB();
    var VL = (from U in dbContext.Users
          join P in dbContext.Products
          on U.PID equals P.PID
          where P.PID == U.PID
          select new UP(){
              UserO = U,
              ProductO = P
          }).Where(U => U.LID == S).ToList();

    return View(VL);
}

模型应该是

public class UP
{
    public User UserO { get; set; }
    public Product ProductO { get; set; }
}

这篇关于在IEnumerable视图中访问两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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