4
votes

So i am building a book search page and everything else works (search by title, author etc) but I cant figure out how to search by the unique number which is an int in our db -- I've tried several things like converting to int32, turning IDnumber into a string etc but still cant find a solution

parameters:

public ActionResult Index(int IDnumber, string LnameString, string FnameString, string bookGenre,string searchString)
{
    //LINQ  query to select books
    var books = from m in db.Book
                select m;
    //ID Number
    if (IDnumber != null)
    /* the if statement gives the following warning which i cannot resolve: the result of the expression is always 'true' since a value of type 'int' is never equal to null of type int?*/
    {
        books = books.Where(x => x.BookID == IDnumber);
    }
...
}

View code:

<p>
@Html.ActionLink("Create New", "Create")

@using (Html.BeginForm("Index", "Books")){
<p>
    Genre: @Html.DropDownList("bookGenre", "All")
    Title: @Html.TextBox("SearchString") <br />
    Author First Name: @Html.TextBox("FnameString")
    Author Last Name: @Html.TextBox("LnameString") <br />
    ID Number: @Html.TextBox("IDNumber")
    <input type="submit" value="Filter" /> 
</p>
}

Exact error:

The parameters dictionary contains a null entry for parameter 'IDnumber' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32, System.String, System.String, System.String, System.String)' in 'SafariBooksGroup15.Controllers.BooksController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

1
books = books.Where(x => x.BookID == Convert.ToInt32(IDnumber)); should just be books = books.Where(x => x.BookID == IDnumber) shouldn't it?wentimo
Oh you are correct, i'll edit sorryParth
IDnumber is already of type int no need to do any converting on that.. take the time to read / debug your codeMethodMan
you can use int? for IDnumber and include it in search if it has value.Reza Aghaei
@MethodMan Ya sorry that didnt make sense there, however fixing it still doesnt address the issueParth

1 Answers

4
votes

You can use int? for IDnumber and include it in search if it has value.

public ActionResult Index(int? IDnumber, string LnameString, string FnameString, string bookGenre,string searchString)
{
    //LINQ  query to select books
    var books = from m in db.Book
                select m;
    //ID Number
    if (IDnumber.HasValue)
    {
        books = books.Where(x => x.BookID == IDnumber.Value);
    }
...
}

Also if you are looking for a simple and extensible pattern for search, I recommend you to separate concerns and use a business logic class and a search model to have an action method like this:

public ActionResult Index(BookSearchModel searchModel)
{
    var business = new BookBusinessLogic();
    var model = business.GetBooks(searchModel);
    return View(model);
}

The answer below describes and implements such pattern: