查看通过控制器查询与 FK 连接的多个模型



这里是控制器代码

    //Approved
                public ActionResult Approved(String VoucherNIK, String Admin, int? month, int? Year, int? minPrice, int? maxPrice)
                {
                    if (Session["Nama_Lengkap"] != null)
                    {
                        var NIKLst = new List<int>();
                        var NIKQry = from d in db.Voucher
                                     where d.Voucher_Number >= 2000
                                     orderby d.NIK
                                     select d.NIK;
                        var query = from app in db.Approved
                                    join det in db.Detail on app.ID_Approved equals det.ID_Approve
                                    join vou in db.Voucher on det.ID_Voucher equals vou.ID_Voucher
                                    join adm in db.Admin on app.ID_Admin equals adm.ID_Admin
                                    select new
                                    {
                                        vou.NIK,
                                        app.Tanggal_Approve,
                                        app.Total,
                                        app.ID_Admin,
                                        adm.Nama_Lengkap,
                                        app.ID_Approved
                                    };
                        IEnumerable < int > enumerable = NIKQry.GroupBy(v => v).Select(group => group.FirstOrDefault());
                        NIKLst = enumerable.ToList();
                        ViewBag.VoucherNIK = new SelectList(NIKLst);
                        try
                        {
                            if (!string.IsNullOrEmpty(Admin))
                            {
                                query = query.Where(s => s.Nama_Lengkap.Contains(Admin));
                            }
                        }
                        catch (DataException /* dex */)
                        {
                            ModelState.AddModelError("", "Unable to Apply.");
                        }
                        return View(query));
                    }
                    else
                    {
                        return RedirectToAction("Login", "Account");
                    }
                }

和风景

        @model IEnumerable<Voucher.Models.Approved>
        @{
            ViewBag.Title = "Approved";
            Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
        }
        @using (Html.BeginForm("Approved", "Voucher", FormMethod.Get))
        {
            <div class="form-horizontal">
                <hr />
                <div class="form-group">
                    NIK Voucher       :
                    <div class="col-md-10">
                        @Html.DropDownList("VoucherNIK", null, "All") <!-- new { onchange = "document.location.href = '?VoucherNIK=' + this.value;" })!-->
                    </div>
                </div>
                <div class="form-group">
                    ADMIN Approve     :
                    <div class="col-md-10">
                        @Html.TextBox("Admin")
                    </div>
                </div>
                <div class="form-group">
                    Month             :
                    <div class="col-md-10">
                        @Html.TextBox("month")
                    </div>
                </div>
                <div class="form-group">
                    Year              :
                    <div class="col-md-10">
                        @Html.TextBox("Year")
                    </div>
                </div>
                <div class="form-group">
                    Price(min NUMBER) :
                    <div class="col-md-10">
                        @Html.TextBox("minPrice")
                    </div>
                </div>
                <div class="form-group">
                    Price(max NUMBER) :
                    <div class="col-md-10">
                        @Html.TextBox("maxPrice")
                    </div>
                </div>
            </div>
                <input type="submit" value="Filter" class="btn-toolbar" /><br />
        }
        <br />
        <table class="table">
            <tr>
                <th>
                    Nama Admin
                </th>
                <th>
                    ID Approved
                </th>
                <th>
                    Tanggal
                </th>
                <th>
                    ID Admin
                </th>
                <th>
                    TOTAL
                </th>
                <th></th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Nama_Lengkap)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ID_Approved)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Tanggal_Approve)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ID_Admin)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Total)
                    </td>
                    <td>
                        @Html.ActionLink("More Detail", "DetailAppr", new { id = item.ID_Approved })
                    </td>
                </tr>
            }
        </table>

运行时我收到此错误

传递到字典中的模型项的类型为"System.Data.Entity.Infrastructure.DbQuery1" ,但此字典需要一个类型为"System.Collections.Generic.IEnumerable1[MvcMarks.Models.MarksType]"的模型项

将语句

return更改为

 return View(query
                 .Select(it => new MarksType{
                     //initilize properties of `MarksType` with `it`     
                 }));

您的视图需要类型为 IEnumerable<MarksType> 的模型query但变量IEnumerable匿名对象。

最新更新