gpt4 book ai didi

c# - 通过来自 Controller 的查询查看与 FK 连接的多个模型

转载 作者:行者123 更新时间:2023-11-30 21:59:24 24 4
gpt4 key购买 nike

这是 Controller 代码

    //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");
}
}

和 View

        @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>

运行时出现这个错误

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1 ,but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MvcMarks.Models.MarksType]'

最佳答案

更改 return声明

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

您的 View 需要类型为 IEnumerable<MarksType> 的模型但是query变量是 IEnumerableAnonymous objects .

关于c# - 通过来自 Controller 的查询查看与 FK 连接的多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43914040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com