【问题标题】:Retrieve data from a linked table and display it in a view从链接表中检索数据并将其显示在视图中
【发布时间】:2018-08-06 01:24:26
【问题描述】:

我有两个模型:

M1:

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

    public partial class M1
    {
        public M1()
        {
            this.M2 = new HashSet<M2>();
        }

        public int Id { get; set; }
        public int N° { get; set; }
        public string Commentaires { get; set; }
        public int Id_qualité { get; set; }

        public virtual Qualité Qualité { get; set; } //*(Qualité est autre table dans ma BDD)


        public virtual ICollection<M2> M2 { get; set; }
    }
}

还有第二个 Model M2:

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

    public partial class M2
    {
        public int Id_M2 { get; set; }
        public string N_wg { get; set; }
        public double Poids { get; set; }

        public int Id { get; set; }

        public virtual M1 M1 { get; set; }
    }
}

还有一个控制器:

namespace wb01.Controllers
{
    using static wb01.Models.M2;

    public class M1Controller : Controller
    {
        private M1Entities db = new M1Entities();

        // GET: M1
        public ActionResult Index()
        { 
            var m1 = db.M1.Include(r => r.Qualité);
            return View(m1.ToList()); 
        }
    }
}

我想在我的视图中显示m1.ToList,其中有一列包含 M1 中每个 Id 的 Poid(意味着 M1 中的 m1_ID 其 Poid=sum(poids mi_ID in M2)。如果有人可以帮助我吗?

【问题讨论】:

  • Salut,恐怕您不会得到任何答案,因为您的示例代码非常清楚。你有 2 张桌子 M1 和 M2 吗?你有身份证可以加入吗?
  • 是2表M1和M2,M1的主键(Id)是表M2的外键。
  • 所以你也许可以加入他们并将结果发送到你的视图中,下面我会给你一个例子

标签: c# asp.net model-view-controller


【解决方案1】:

所以也许你可以尝试加入你的 2 张桌子:

public ActionResult Index()
{
    var query = db.M1    // your starting point - table in the "from" statement
        .Join(db.M2, // the source table of the inner join
        t1 => t1.id,        // Select the primary key (the first part of the "on" clause in an sql "join" statement)
        t2 => t2.id,   // Select the foreign key (the second part of the "on" clause)
        (t1, t2) => new { t1 = t1, t2 = t2 }) // selection
        .Where(M1AndM2 => M1AndM2.t1.id == 1);    // where statement

    return View(m1.ToList());
}

【讨论】:

  • 我有一个错误 :( :"System.Collections.Generic.List1[&lt;&gt;f__AnonymousType1 ...« System.Collections.Generic.IEnumerable1[wb01.Models."; 我用 :@model IEnumerable
【解决方案2】:

你可以像这样在你需要的列上投影

var m1 = db.M1.Include(r => r.M2).Include(r => r.Qualité).Select(r => new {
    Poids = r.M2.Sum(x => x.Poids),
    Col1 = r.Col1,
    Col2 = r.Col2,
    Col3 = r.Col3,
    Col4 = r.Col4
});
return View(m1.ToList());

【讨论】:

  • 谢谢 :) ,我尝试使用您的代码,但在尝试索引视图时出现错误:“System.InvalidOperationException: Échec du cast en type valeur 'System.Double', car la valeur matérialisée est Null"
  • @lot 你确定你在 M2 中没有 null poids 值吗如果有 null poids 值那么你需要过滤非 null Poids = r.M2.Where(x =&gt; x.Poids != null).Sum(x =&gt; x.Poids),
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多