【问题标题】:The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[TipoDeCanal]',传入字典的模型项的类型为“System.Collections.Generic.List`1[TipoDeCanal]”,
【发布时间】:2016-01-07 16:54:50
【问题描述】:

将数据从控制器传递到视图时出现此错误:

传入字典的模型项的类型为“System.Collections.Generic.List`1[TipoDeCanal]”,但该字典需要类型为 TipoDeCanal 的模型项。

     public class TipoDeCanalesController : GenericController
        {
            private UnitOfWork unitOfWork = new UnitOfWork();

            // GET: TipoDeCanales
            public ActionResult Index([DataSourceRequest] DataSourceRequest request)
            {
                //return Json(unitOfWork.TipoDeCanalRepository.Get(),JsonRequestBehavior.AllowGet);
                return View(unitOfWork.TipoDeCanalRepository.Get());
            }


@model ..Models.TipoDeCanal
@using ..Models
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@(Html.Kendo().Grid<TipoDeCanal>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ID);
        columns.Bound(p => p.Nombre).Title("Nombre");
        columns.Bound(p => p.Descripcion).Title("Descripcion");
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .Pageable()
    .Sortable()
    .Scrollable(scr => scr.Height(430))
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(p => p.ID))
        .Create(update => update.Action("EditingPopup_Create", "Grid"))
        .Read(read => read.Action("Index", "Grid"))
        .Update(update => update.Action("EditingPopup_Update", "Grid"))
        .Destroy(update => update.Action("EditingPopup_Destroy", "Grid"))
    )
)
<script type="text/javascript">
    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
</script>

【问题讨论】:

  • 好吧,大概unitOfWork.TipoDeCanalRepository.Get() 返回一个List&lt;TipoDeCanal&gt;... 而您的视图只需要一个项目。你期望它做什么?

标签: c# asp.net-mvc asp.net-mvc-3 generics


【解决方案1】:

您的视图需要单个对象 @model ...Models.TipoDeCanal 但您的控制器操作显然返回 IEnumerable&lt;TipoDeCanal&gt;

将其替换为@model List&lt;...Models.TipoDeCanal&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    相关资源
    最近更新 更多