C#MVC视图模型使用联接表返回IEnumerable列表

时间:2018-11-21 04:39:06

标签: c# linq model-view-controller

当我从事我的项目并从每个步骤中学到东西时,我仍然陷于某些部分。我有3个表SubJobs,MaterialRequired和Parts。所需材料实际上只是一个参考表。我需要生成的是SubJob Detail,然后是零件列表。因此,Subjob有很多零件,我想保持零件表清洁,而不必在零件表中添加重复数据。例如,SubJob号102311可具有零件号Ef3012,而另一个SubJob可具有相同零件。所以我不想在表中使用不同的SubJobs拥有相同的零件号5次。因此,我制作了一个MaterialRequired表,其中包含SubJob号和PartNumber都是其他表的键。我在页面上输入一个SubJob编号,并在ViewModel中显示数据。该页面加载了SubJob详细信息,但显示零件时出现问题。在下面的代码结果中,RequiredDetail会根据需要从MaterialsRequired生成一个零件编号列表。我仅使用此代码来了解其工作方式。当前形式的result.PartsDetail仅产生1条记录,而不是该子作业的所有记录。在模型本身中我缺少什么吗?还是有另一种方法来获取我所需的列表结果?

这是我的ViewModel:

    public partial class SubJobDetails
{
    public static SubJobDetails GetSubjobsAndParts(string mReq, CustomerEntities db)
    {
        var Parts = from pts in db.Parts
                   join mr in db.MaterialRequired on pts.PartNumber equals mr.Material
                   join sj in db.SubJobs on mr.SubJob equals sj.JobNumber
                   where (mr.SubJob == mReq)
                   select new SubJobDetails()
                   {
                       Parts = pts,
                       Material = mr,
                       SubJobs = sj
                   };

        var result = Parts.FirstOrDefault();

        if (result != null)
        {
            result.RequiredDetail = db.MaterialRequired.Where(a => a.SubJob == result.SubJobs.JobNumber);
            result.PartsDetail = db.Parts.Where(a => a.PartNumber == result.Material.Material);
        };

        return result;
    }

    public virtual Parts Parts { get; set; }
    public virtual SubJobs SubJobs { get; set; }
    public virtual MaterialRequired Material { get; set; }
    public virtual IEnumerable<Parts> PartsDetail { get; set; }
    public virtual IEnumerable<MaterialRequired> RequiredDetail { get; set; }

这是控制器(我相信它正在按预期工作):

        public ActionResult PartsDetail(string mReq)
    {
        if (mReq == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        SubJobDetails modelInstance = SubJobDetails.GetSubjobsAndParts(mReq, db);
        //if (modelInstance == null)
        //{
        //    return HttpNotFound();
        //}
        return View(modelInstance);

这是我的查看页面:

@model BestenEquipment.Models.SubJobDetails
@{
   ViewBag.Title = "PartsDetail";
   Layout = "~/Views/Shared/CustomerDashboardLayout.cshtml";
}

<div id="page-wrapper">
<div class="row">
    <div class="col-lg-12">
        <div>
            <h4>Sub Job</h4>
            <hr />
            <dl class="dl-horizontal">

                <dt>
                    @Html.DisplayNameFor(model => model.SubJobs.Description)
                </dt>

                <dd>
                    @Html.DisplayFor(model => model.SubJobs.Description)
                </dd>

                <dt>
                    @Html.DisplayNameFor(model => model.SubJobs.ExtDescription)
                </dt>

                <dd>
                    @Html.DisplayFor(model => model.SubJobs.ExtDescription)
                </dd>

                <dt>
                    @Html.DisplayNameFor(model => model.SubJobs.PartNumber)
                </dt>

                <dd>
                    @Html.DisplayFor(model => model.SubJobs.PartNumber)
                </dd>

                <dt>
                    @Html.DisplayNameFor(model => model.SubJobs.Drawing)
                </dt>

                <dd>
                    @Html.DisplayFor(model => model.SubJobs.Drawing)
                </dd>
            </dl>
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div>
                    <h4>Parts</h4>
                    <table class="table">
                        <tr>
                            <th>
                                @Html.DisplayNameFor(model => model.Parts.PartNumber)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.Parts.Description)
                            </th>

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

                            <th>
                                @Html.DisplayNameFor(model => model.Parts.DrawingNumber)
                            </th>
                        </tr>
                        @foreach (var item in Model.PartsDetail)
                        {
                            <tr>
                                <td>
                                    @Html.DisplayFor(model => item.PartNumber)
                                </td>

                                <td>
                                    @Html.DisplayFor(model => item.Description)
                                </td>

                                <td>
                                    @Html.DisplayFor(model => item.ExtDescription)
                                </td>

                                <td>
                                    @Html.DisplayFor(model => item.DrawingNumber)
                                </td>
                                <td></td>
                            </tr>
                        }
                    </table>
                </div>
            </div>
        </div>
    </div>
    <!-- /.col-lg-12 -->
</div>
</div>

1 个答案:

答案 0 :(得分:0)

以下是上述问题的解决方案。首先,我使用了MaterialsRequired和Parts并制作了一个SQL视图。创建了一个名为PartsView的模型“ SQL View的名称”,此时我只需要进行1个联接,但是我确实向该联接添加了另一个联接,以使我可以将按钮链接返回到顶级Job“ MachineDetail”页面。 / p>

这是ViewModel:

    public partial class SubJobDetails
{
    public static SubJobDetails GetSubjobsAndParts(string mReq, CustomerEntities db)
    {

        var Parts = from pts in db.PartsView
                    join sj in db.SubJobs on pts.SubJob equals sj.SubJob
                    join tlj in db.TopLvlJobs on sj.TopLvlJob equals tlj.TopLvlJob
                    where (pts.SubJob == mReq)
                   select new SubJobDetails()
                   {
                       PartsView = pts,
                       TopLvlJob = tlj.TopLvlJob,
                       SubJobs = sj,

                   };

        var result = Parts.FirstOrDefault();

        if (result != null)
        {
            result.PartsDetail = db.PartsView.Where(a => a.SubJob == result.PartsView.SubJob);
        };

        return result;
    }
    public string TopLvlJob { get; set; }
    public virtual TopLvlJobs TopLvlJobs { get; set; }
    public virtual PartsView PartsView { get; set; }
    public virtual SubJobs SubJobs { get; set; }
    public virtual IEnumerable<PartsView> PartsDetail { get; set; }
}

控制器动作保持不变。剃须刀页面保持不变,只是在foreach中添加了“ if”语句以仅显示IsViewable = true的记录。