【问题标题】:access two models in a IEnumerable view在 IEnumerable 视图中访问两个模型
【发布时间】:2019-10-20 09:55:20
【问题描述】:

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

用户模型

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; }
    }
}

我在用户控制器中有详细信息操作

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' 的公共实例定义。

【问题讨论】:

  • 以后,在此处的答案解决了您最初的答案后,请勿更改您问题的性质。一次一个问题。

标签: c# asp.net-mvc entity-framework


【解决方案1】:

您的查询应该是这样的:

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; }
}

【讨论】:

  • 我已经更新了我的动作和 UP 类,现在问题在细节视图中。
  • 没错,你应该像这样访问模型的属性@Html.DisplayNameFor(model =&gt; model.UserO.UserName)
【解决方案2】:

从 UP 中删除 IEnumerable

@model SLMDemo0.Models.UP

【讨论】:

    【解决方案3】:

    谢谢大家,它在如下视图中工作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多