Dharamveer Grewal
Dharamveer Grewal

Reputation: 135

Index method not accepting parameters in ASP.NET Core MVC with Entity Framework

I am working on a webpage where I have to filter results using value from the select menu's selected option when the user clicks on filter button by sending a get parameter to the index method.

Upvotes: 1

Views: 295

Answers (2)

Fei Han
Fei Han

Reputation: 27793

refId is never passed into the Index method

In your code, we can find that you specify asp-for="@Model.FirstOrDefault().RefereeId" for your <select> tag, if you check the source code of this dropdown in browser, you would find name attribute value is RefereeId (not refId) as below. So it would pass something like RefereeId=168 through querystring to your action, and parameter refId of your action would always be 0, which cause the issue.

enter image description here

To fix it, you can try to rename your action parameter to RefereeId, like below.

public async Task<IActionResult> Index(int RefereeId)
{
    //code logic here

Or modify view page code to set name of <select> tag with "refId".

<select name="refId" asp-items="@ViewBag.PersonId"></select>

Upvotes: 1

Jerdine Sabio
Jerdine Sabio

Reputation: 6130

If you don't have a custom route defined, the default routing in asp.net core uses id.

Replace the refId parameter to id.

public async Task<IActionResult> Index(int id)
{
    // ...

    if (id != 0) //always remains 0
    {
        games = games.Where(g => g.RefereeId == id);
    }

    // ...
}

Upvotes: 0

Related Questions