通过Controller中的查询查看与FK一起加入的多个模型

时间:2017-05-11 11:24:49

标签: c# mysql asp.net-mvc asp.net-mvc-4

这是控制器代码

$client=new SoapClient('http://www.***.co.uk/external/?r=api/service');

和视图

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

运行时出现此错误

  

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

1 个答案:

答案 0 :(得分:0)

return语句更改为

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

您的视图需要IEnumerable<MarksType>类型的模型,query变量IEnumerable的{​​{1}}。