2017-05-11 1 views
1

est ici Code contrôleurVoir le modèle multiple qui JOIN avec FK par requête du contrôleur

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

Et la vue

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

Lorsque Run i a cette erreur

L'élément de modèle passé dans le dictionnaire est de type 'System.Data.Entity.Infrastructure.DbQuery1 , mais ce dictionnaire nécessite un modèle ite m de type 'System.Collections.Generic.IEnumerable1 [MvcMarks.Models.MarksType]'

Répondre

0

Modifier déclaration return à

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

Votre vue attend un modèle de type IEnumerable<MarksType> mais query variable est de IEnumerableAnonymous objects.