0

I'm a newbie in MVC, so please help me to get out of this. I want to retrieve NameofBike in DB. I'm using LINQ for it, but something wrong in there. My Action:

public ActionResult Index(int Id, string Name)
{
     // Create our view model  
    var viewModel = BikesDB.ProductSubcategories
        .Where(m => m.NameofBike == Name && m.ProductSubcategoryID == Id);

    return this.View(viewModel);
}

ViewModel:

public class Bike
{
    public int Id { get; set; }
    public String Name { get; set; }
}

public class CategoriesIndexViewModel
{
    public int NumberOfModel { get; set; }
    public List<string> NameofBike { get; set; }
    public List<Bike> Bikes { get; set; }
}

When debugged it throws an error like this:

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

Here's the navigate:

<ul id="navlist">
            <li>
                <a href="@Url.Content("~/Bike/")">Home</a>
                <ul>
                    <li>
                        <a href="@Url.Content("~/Bike/Categories/1?name=Mountain Bikes&class=image")">Mountain Bikes</a>
                    </li>
                    <li>
                        <a href="@Url.Content("~/Bike/Categories/2?name=Road Bikes&class=image")">Road Bikes</a>
                    </li>
                    <li>
                        <a href="@Url.Content("~/Bike/Categories/3?name=Touring Bikes&class=image")">Touring Bikes</a>
                    </li>
                </ul>
            </li>
Trung Pham
  • 233
  • 2
  • 6
  • 19

3 Answers3

0

I think you ID value contains some null value. try this links it may help you

MVC 3 The parameters dictionary contains a null entry for parameter 'id' of non-nullable type

Parameters dictionary contains a null entry for parameter

Community
  • 1
  • 1
Golda
  • 3,823
  • 10
  • 34
  • 67
0

Basically your Id parameter is missing from the form POST.

Put a breakpoint on your Index action method on the line:

 `var viewModel = Bikes....` 

and run the project in debug. When the breakpoint is hit, hover over the int Id & string Name params and you'll see what is actually being passed to your method.

markpsmith
  • 4,860
  • 2
  • 33
  • 62
0

You need to generate urls via UrlHelper.Action method so they match you route configuration. Also you can create links via LinkExtensions.ActionLink.

@Html.ActionLink("Home", "Index", "Bike",
     new { Id = /* categoryId here */, Name = /* name of a bike */ })
Zabavsky
  • 13,340
  • 8
  • 54
  • 79