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