简体   繁体   中英

'System.Collections.Generic.List`1[<>f__AnonymousType1 but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable ERROR

Controller

public ActionResult Index()
{
   try
   {
      using (var DB =  new CekTakipOtomasyonEntities())
         {
            var model = (from f in DB.FirmaCek
                         join k in DB.KullaniciHesap
                         on f.KullaniciID equals k.ID
                         select new
                         {
                             f.ID,
                             f.Tutar,
                             f.KayitTarihi,
                             f.VadeTarihi,
                             tutar = f.Tutar,
                             f.SeriNo,
                             f.Banka,
                             f.Sube,
                             f.Iban,
                             f.Kesideci,
                             f.CiroEdilen,
                             f.TeslimEden,
                             k.Ad,
                             k.Soyad
                         }).OrderByDescending(f => f.KayitTarihi).ToList();
            if (model == null) return View();
            return View(model);
        }
    }
    catch { return View(); }
}

View

@using CekTakipOtomasyon.Models
@model IEnumerable<FirmaCek>

@foreach (var firma in Model)
                            {
                                <tr>
                                    <td><input type="checkbox" name="secili" value="@firma.ID" /></td>
                                    <td>@firma.KayitTarihi.ToLongDateString()</td>
                                    <td>@firma.VadeTarihi.Value.ToLongDateString()</td>
                                    <td>@Convert.ToInt32(firma.Tutar).ToString() TL</td>
                                    <td>@firma.SeriNo</td>
                                    <td>@firma.Banka</td>
                                    <td>@firma.Sube</td>
                                    <td>@firma.Iban</td>
                                    <td>@firma.Kesideci</td>
                                    <td>@firma.CiroEdilen</td>
                                    <td>@firma.TeslimEden</td>
                                    <td>@firma.KullaniciHesap.Ad @firma.KullaniciHesap.Soyad</td>
                                    <td><button id="btn_Guncelle" value="@firma.ID" class="btn btn-block btn-info">Güncelle</button></td>
                                </tr>
                            }

I think I am misrepresenting variable type in view, what is the solution? Thank you from now

Model must be known or understandable type in your view. Change it as

@model IEnumerable<FirmaCek> or @model IEnumerable<YourModelTypeInYourController> . model is sent by Index action and it should have same type in the view.

You're declaring your model as IEnumerable but try to use it with a List of anonymous types.

Your options are:

  1. Declare it as IEnumerable of YourType and create instances of this type in your Select clause. Currently it creates an anonymous object.

Here's an example of non-anonymous creation:

                     var model = (from f in DB.FirmaCek
                                 join k in DB.KullaniciHesap
                                 on f.KullaniciID equals k.ID
                                 select new MyViewModel
                                 {
                                     ID = f.ID //add properties here                              
                                 }).OrderByDescending(f => f.KayitTarihi).ToList();
  1. If you want to keep using anonymous object, use IEnumerable together with ExpandoObject like this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM