【问题标题】:MVC System.InvalidOperationException: but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[smoethng]MVC System.InvalidOperationException:但此字典需要“System.Collections.Generic.IEnumerable`1[smoethng] 类型的模型项
【发布时间】:2013-05-08 04:12:40
【问题描述】:

塞拉姆,

public ActionResult Edit()
        {
            var model = new HomeViewModel();
            using (var context = new BannerEntities())
            {
                model.FotoList = context.Fotos.ToList();
                return View(model);
            }
        }

视图是

@model IEnumerable<Banner.Models.HomeViewModel>

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

我正在接受错误: 传入字典的模型项的类型为“Banner.Models.HomeViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[Banner.Models.HomeViewModel]”的模型项。 有什么想法为什么会出现这个问题?

【问题讨论】:

    标签: asp.net-mvc razor


    【解决方案1】:

    您正在传递Banner.Models.HomeViewModel 的单个实例,但您的视图期待IEnumerableBanner.Models.HomeViewModel 集合。

    将您的视图更改为如下所示,它应该可以工作:

    @model Banner.Models.HomeViewModel
    
    @{
        ViewBag.Title = "Edit";
    }
    
    <h2>Edit</h2>
    

    【讨论】:

      猜你喜欢
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2015-09-19
      • 2013-10-17
      相关资源
      最近更新 更多