Click here to Skip to main content
15,888,979 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Index.cshtml page has text box and search button , when we click search button the value of the text box needs to be assigned to action method parameter , The action method searchById is in Home controller,

I am getting below error ,

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


What I have tried:

Index.cshtml page

@model IEnumerable< ModelBinding.Models.Employee>
           

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("SearchById","Home"))
    {
        <p>
            <label>Enter ID:<input type="number" id="Id" /></label>
        </p>
        <button>Search</button>
    }


Home controller SearchbyId method with integer parameter:

public ActionResult SearchById(int Id)
       {
           return View("Index", empList.Where(x => x.Id == Id));
       }
Posted
Updated 18-Dec-17 8:27am

Hello Meiyarasan,

Do changes on your view:

replace
@model IEnumerable< ModelBinding.Models.Employee>
to
@model ModelBinding.Models.Employee

and

to
@html.textboxfor(m=>m.Id,new(ID="ID"))
 
Share this answer
 
HTML form controls only send their value to the server if they have a name attribute. Your <input> only has an id attribute, so the value won't be submitted.
HTML
<label>Enter ID: <input type="number" id="Id" name="Id" /></label>

HTML input tag[^]
 
Share this answer
 

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