没有类型为“IEnumerable”的ViewData项具有键“艺术家”和“艺术家”。 [英] There is no ViewData item of type 'IEnumerable' that has the key "Artist"

查看:92
本文介绍了没有类型为“IEnumerable”的ViewData项具有键“艺术家”和“艺术家”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几张桌子的数据库。



Table Albums = AlbumId,Gen​​reId,ArtistId,Title,Price和AlbumArt。

Table Artits = ArtistId,Name。

表格类型= GenreId,名称。



我有一个商店库存页面,列出所有相册并允许CRUD。

然后我有一个用过滤器搜索数据库的表单。此表单包含所有艺术家的下拉列表,因此用户可以将其缩小为一位艺术家。然后是一个文本框,接受一个针对相册标题搜索的字符串。



我的搜索字符串有效,但我不能让下拉列表工作。页面甚至不会加载。我收到上面的错误信息。



这是我的相关代码



控制器



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 System.Data;
使用 System.Data.Entity;
使用 WebsiteManager.Models;

namespace WebsiteManager.Controllers
{

public ActionResult SearchIndex( string 艺术家,字符串 searchString)
{
var ArtistLst = new List< string>();

var ArtistQry = 来自 d in db.Artists
orderby d.Name
选择 d.Name;
ArtistLst.AddRange(ArtistQry.Distinct());
ViewBag.Artists = new SelectList(ArtistLst);

var albums = 来自 m in db.Albums
选择 m;

if (!String.IsNullOrEmpty(searchString))
{
albums = albums.Where(s = < span class =code-keyword>> s.Title.Contains(searchString));
}

if string .IsNullOrEmpty(Artists))
return 查看(相册);
else
{
return 查看(专辑。所在地(x) = > x.Name ==艺术家));
}

}
}







查看



 @ model IEnumerable< WebsiteManager.Models.Album> 

< aside style = width:800px >
< h2>音乐列表< / h2 >
< p>
@ Html.ActionLink( Create new 创建 new {@ class = button})|
@ Html.ActionLink( 购物车 索引 ShoppingCart


@using(Html.BeginForm( SearchIndex StoreManager,FormMethod.Get))
{
< span>
艺术家:@ Html.DropDownList( 艺术家 全部
专辑:@ Html.TextBox( SearchString
< input type = submit value = 搜索 /> < / span >
}
< / p >

< table>
< tr>
< th>类型< / th >
< th>艺术家< / th >
< th>标题< / th >
< th>价格< / th >
< th> < / th >
< / tr >
@foreach( var item in Model){
< tr>
< td> @ Html.DisplayFor(modelItem = > item.Genre.Name)< / td >
< td style = width:205px> @ Html.DisplayFor(modelItem = > item.Artist.Name)< / td >
< td style = width:285px> @ Html.DisplayFor(modelItem = > item.Title)< / td >
< td style = < span class =code-string> text-align:right> @ Html.DisplayFor(modelIt em = > item.Price)< / td >

< td> @ Html.ActionLink( 编辑 编辑 new {id = item.AlbumId})|
@ Html.ActionLink( 详细信息 详细信息 new {id = item.AlbumId})|
@ Html.ActionLink( 删除 删除 new {id = item.AlbumId})
< / td >
< / tr >
}
< / table >

< / 搁置 >





可能导致此问题?谢谢。



p.s.如果你需要更多代码,只需告诉我你想看到什么

解决方案

我从不使用viewbag,我总是写一个强类型模型。但是,我很清楚你的收藏品不是艺术家,而是字符串。这可能是错误的意思,但是如果你使用了Model类,我打赌你会得到更清晰的错误信息。



当然,如果你将您的LINQ更改为



  var  ArtistQry = 来自 d   db.Artists 
orderby d。姓名
选择 d;





我认为这也会修复它,但是你需要更改你的下拉列表来显示名称,或者让你的类在调用ToString时返回Name属性(如果这是EF并自动生成,你可以使用扩展方法)。 / BLOCKQUOTE>

I have a database with a few tables.

Table Albums = AlbumId, GenreId, ArtistId, Title, Price, and AlbumArt.
Table Artits = ArtistId, Name.
Table Genre = GenreId, Name.

I have a Store inventory page that lists all the albums and allows CRUD.
and then I have a form that searches through the database with a filter. This form has a dropdown list of all artists so the user can narrow it down to one artist. And then a textbox that accepts a string that is searched against the album titles.

My searchstring works, but I can''t make the dropdown list work. The page won''t even load. I get the error message above.

Here''s my related code

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using WebsiteManager.Models;

namespace WebsiteManager.Controllers
{

public ActionResult SearchIndex(string Artists, string searchString) 
        { 
            var ArtistLst = new List<string>(); 
 
            var ArtistQry = from d in db.Artists 
                           orderby d.Name 
                           select d.Name; 
            ArtistLst.AddRange(ArtistQry.Distinct()); 
            ViewBag.Artists = new SelectList(ArtistLst); 
 
            var albums = from m in db.Albums 
                         select m; 
 
            if (!String.IsNullOrEmpty(searchString)) 
            { 
                albums = albums.Where(s => s.Title.Contains(searchString)); 
            } 
 
            if (string.IsNullOrEmpty(Artists))
                return View(albums); 
            else 
            {
                return View(albums.Where(x => x.Name == Artists)); 
            } 
 
        }       
}




View

@model IEnumerable<WebsiteManager.Models.Album>

<aside style="width: 800px">
<h2>Music List</h2>
<p>
    @Html.ActionLink("Create New", "Create", new { @class="button" }) |
    @Html.ActionLink("Shopping Cart", "Index", "ShoppingCart")


    @using (Html.BeginForm("SearchIndex","StoreManager",FormMethod.Get))
    {    
        <span>
            Artist: @Html.DropDownList("Artists", "All")          
            Album: @Html.TextBox("SearchString") 
         <input type="submit" value="Search" /></span> 
    }
</p>

<table>
        <tr>
            <th>Genre</th>
            <th>Artist</th>
            <th>Title</th>
            <th>Price</th>
            <th></th>
        </tr>
    @foreach (var item in Model) {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.Genre.Name)</td>
            <td style="width:205px">@Html.DisplayFor(modelItem => item.Artist.Name)</td>
            <td style="width:285px">@Html.DisplayFor(modelItem => item.Title)</td>
            <td style="text-align:right">@Html.DisplayFor(modelItem => item.Price)</td>
            
            <td>@Html.ActionLink("Edit", "Edit", new { id=item.AlbumId }) |
                @Html.ActionLink("Details", "Details", new { id=item.AlbumId }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.AlbumId })
            </td>
        </tr>
    }
</table>

</aside>



what could be causing this? Thanks.

p.s. if you need more code, just tell me what you want to see

解决方案

I never use the viewbag, I always write a strongly typed Model. However, it''s clear to me that your collection is not of artists, but of strings. That''s probably what the error means, but if you used a Model class, I bet you''d get clearer error messages.

Of course, if you change your LINQ to

var ArtistQry = from d in db.Artists
                          orderby d.Name
                          select d;



I think that will also fix it, but then you''ll need to change your drop list to show the name, or make your class return the Name property when ToString is called ( if this is EF and auto generated, you can use an extension method ).


这篇关于没有类型为“IEnumerable”的ViewData项具有键“艺术家”和“艺术家”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆