Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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

C#
@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
Posted

1 solution

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

C#
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 ).
 
Share this answer
 
v2
Comments
[no name] 1-Jun-13 18:47pm    
Thank Your Christian. I made that change and I got the line below <<<(ArtistLst.AddRange(ArtistQry.Distinct()); >>> lit up red. I commented it out and nothing else was dependent so I ran it both ways, with Name in the dropdown and with artists and the same error kept coming on.

SOrry, I am a real noob at ASP. This is a tutorial that I am following from ASP.NET. Would you help me with a little more details. For instance, what do I need to do to run it through a Model insead of a viewbag?

Also. I noticed that the data is spread out through all three databases and using relationships but this causes a problem because the Album table for instance, calls the Artist field by artistId. Which means when I search in the form, I get the artist Id instead of it's name. How could I fix something like that?
Christian Graus 1-Jun-13 19:14pm    
A viewbag is almost always a waste of time. Look in to using Model classes in your views, and you should find everything works from there. The way to fix your other issue is again, to use a strongly typed list of artists, and then explicity show the Name property of the Artist object.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900