简体   繁体   English

字典需要System.Collections.Generic.List类型的模型项

[英]Dictionary requires a model item of type System.Collections.Generic.List

I have a layout.csthml which contains a sidebar and also contains a @Renderbody. 我有一个layout.csthml,其中包含一个侧边栏,还包含一个@Renderbody。 Both the @Renderbody and the sidebar use the following in their individual views. @Renderbody和侧边栏在其各自的视图中均使用以下内容。

@model List<appstowindows.Models.apps>
@foreach (var item in  Model){...}

Both views render the list fine, but when I try to open the edit URL in the view I keep getting the error: 两种视图都可以很好地显示列表,但是当我尝试在视图中打开编辑URL时,仍然出现错误:

The model item passed into the dictionary is of type System.Data.Entity.DynamicProxies, Dictionary requires a model item of type System.Collections.Generic.List 传递到字典中的模型项的类型为System.Data.Entity.DynamicProxies,字典需要类型为System.Collections.Generic.List的模型项

Edit url 编辑网址

@Html.ActionLink("Edit", "Edit", new { id = item.app_key }) 

Index 指数

    public ActionResult Index()
    {
        var applist = db.apps.Include(a => a.appgroups);
        applist = db.apps.Include(a => a.appstatus);

        return View(applist.ToList());
    }

Edit 编辑

    public ActionResult Edit(int? id)
    {
        apps apps = db.apps.Find(id);
        return View(apps);
    }

Important Note: If I would remove either one of the lists in one of the views the edit URL works fine. 重要说明:如果我要删除其中一个视图中的任一列表,则编辑URL可以正常工作。

What is causing this error and how to resolve? 是什么导致此错误以及如何解决?

update: 更新:

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

@using (Html.BeginForm("Edit", "Apps", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>apps</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.app_key)

</div>
}

You are passing model of wrong type to your view. 您正在将错误类型的模型传递给视图。 Passed object is of type System.Data.Entity.DynamicProxies while System.Collections.Generic.List is required. 传递的对象的类型为System.Data.Entity.DynamicProxiesSystem.Collections.Generic.List是必需的。 Check which object is provided as an argument to it. 检查提供哪个对象作为其参数。

UPDATED: And the problem is with model specified at your layout page. 更新:问题出在您的布局页面上指定的model Edit view is based on layout page, so it requires a model of type List<apps> to be able to render. Edit视图基于布局页面,因此它需要List<apps>类型的模型才能呈现。 But Edit view requires apps as model on the opposite side. 但是“ Edit视图”要求apps在相反的一侧作为模型。 And while there is no type, which is List<apps> and apps at the same time, your code won't work. 虽然没有类型,即List<apps>apps同时存在,但是您的代码将无法工作。

Generally, you should avoid layout page typing, because all your views, based on that layout, will require the same or descendant type of model. 通常,应避免键入布局页面,因为基于该布局的所有视图都将需要相同或后代的模型类型。 If all of your pages should really share some information, you could solve this problem with multiple options: 如果您的所有页面都应该真正共享一些信息,则可以使用多种选项解决此问题:

  1. First one (and the worst) - base viewmodel type, which will be used as model for layout. 第一个(也是最差的)-基本viewmodel类型,将用作布局模型。 In this solution all of views (which are based on this layout) should have models of base viewmodel descendant type, ie all actions have to get data for base viewmodel as well as for view model itself. 在此解决方案中,所有视图(基于此布局)都应具有基本viewmodel后代类型的模型,即所有操作都必须获取基本viewmodel以及视图模型本身的数据。
  2. Second one (I will go with it for simple cases) - child action, invoked inside your layout. 第二个(在简单的情况下,我将使用它)-子动作,在您的布局内调用。 It's better than previous, because data for shared view part should be retrieved only once in child action, there are no restrictions for views model type and your layout could stay without model. 它比以前的要好,因为共享视图部件的数据在子操作中应该只检索一次,因此视图模型类型没有限制,并且布局可以不使用模型。 It uses composition instead of inheritance, which is simpler for supporting and is a preferable way of making things (you will be able to easily add multiple blocks of shared information with this approach, try to achieve it with first one). 它使用组合而不是继承,后者更易于支持,并且是一种更可取的制作方式(您可以通过这种方法轻松添加多个共享信息块,并尝试使用第一个实现)。 But child actions are restricted in MVC, they cannot be async (there are some hacks to make them async , but it's not a case) 但是子动作在MVC中受到限制,它们不能是async (有一些技巧可以使它们async ,但事实并非如此)
  3. Third one (I will go with it, if multiple blocks of shared information should be displayed) - using js for deferred loading of content blocks. 第三个(如果应该显示多个共享信息块,我将继续使用)-使用js延迟加载内容块。 Has the same advantages as previous, but actions can be async by default. 具有与先前相同的优点,但是默认情况下动作可以是async的。 Also those blocks will not delay page loading and could be decorated with spinners or something in that way. 同样,这些块不会延迟页面加载,并且可以用微调框或类似方式装饰。

In your case error is caused by @model List<appstowindows.Models.apps> . 在您的情况下,错误是由@model List<appstowindows.Models.apps>引起的。 While Edit view is executed, it renders layout page markup first, so object of type apps , provided as model to Edit view, is not suitable. 执行“ Edit视图时,它首先呈现布局页面标记,因此不适合作为模型提供给“ Edit视图的类型为apps对象。 I suppose, you specified a type at your layout, because some markup is shared between all of your pages (if I'm wrong, just delete @model from your layout, and everything will be okay) and this sharing could be implemented by one of methods I've proposed earlier. 我想,您在布局中指定了一种类型,因为在所有页面之间共享了一些标记(如果我错了,只需从布局中删除@model ,一切都会好起来的),并且这种共享可以由一个人实现我之前提出的方法

If you go with the second one, you should add an action to your controller (don't know what information you are rendering, so I will call it Foo ) 如果您选择第二个,则应该向控制器添加一个动作(不知道您要渲染什么信息,因此我将其称为Foo

public ActionResult Foo()
{
    /*get data for your shared content part*/

    return PartialView(/*provide data here*/);
}

Then create View for it with markup, which should be shared. 然后为其创建带有标记的View ,应将其共享。 (The one from your layout, which causes issue) (来自您布局的那一个,会引起问题)

@model List<appstowindows.Models.apps>
@foreach (var item in  Model){...}

Then remove that markup from your layout and call @Html.Action("Foo") instead. 然后从您的布局中删除该标记,然后调用@ Html.Action(“ Foo”)

Btw, I suppose you should learn some information about layout pages and models in ASP.MVC, official website is a good starting point. 顺便说一句,我想您应该学习有关ASP.MVC中的布局页面和模型的一些信息,官方网站是一个很好的起点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List,但是此字典需要类型为的模型项 - model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type &#39;System.Collections.Generic.List`1 [&lt;&gt; f__AnonymousType1,但此字典需要类型为&#39;System.Collections.Generic.IEnumerable的模型项 - 'System.Collections.Generic.List`1[<>f__AnonymousType1 but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable ERROR 传递到字典中的模型项的类型为“ System.Collections.Generic.List” - The model item passed into the dictionary is of type 'System.Collections.Generic.List` 传递到字典中的 model 项目类型为“System.Collections.Generic.List”1 需要类型为“.Generic.IEnumerable”的 model 项目 - The model item passed into the dictionary is of type 'System.Collections.Generic.List'1 requires a model item of type '.Generic.IEnumerable 流行的模型项传递到类型为System.Collections.Generic.List`1 [X]的字典中,但是此字典需要类型为X的模型项 - The popular model item passed into the dictis of type System.Collections.Generic.List`1[X] but this dictionary requires a model item of type X 传递到字典中的模型项的类型为“ System.Collections.Generic.List`1 [System.String]”,但要求类型为“其他” - The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]' but requires type “Something else” 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List`1 [System.String] - The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String] 异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“ System.Collections.Generic.List”。 - Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List` 传递到字典中的模型项在ASP.net MVC中的类型为“ System.Collections.Generic.List…” - The model item passed into the dictionary is of type 'System.Collections.Generic.List… in ASP.net MVC ASP.Net MVC“传入字典的模型项是&#39;System.Collections.Generic.List&#39;类型” - ASP.Net MVC “The model item passed into the dictionary is of type 'System.Collections.Generic.List”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM