Search code examples
.netasp.net-coremodel-view-controllerrazor

I am unable to get the values from a form from cshtml page to my controller


I am getting null values in my controller after the form is submitted to the code

<form action="Home/Book" method="Post">
   <input type="text" id="Name" placeholder="Name">
   <input id="Email" type="text" placeholder="Email">
   <input id="Number"type="number" placeholder="Mobile Number">
   <textarea id="Comment" placeholder="comment here"></textarea>
   <button type="submit">Submit</button>
</form>

This is my controller code

public IActionResult Book(Book obj)
{
string name =obj.Name;
name = name + "testing if value is there";
return View();
}

enter image description here


Solution

  • You need to use name attribute to bind data in the form, please change your input to:

    <input type="text" id="Name" name="Name" placeholder="Name">
    

    or if you are using asp.net core mvc, You can use tag hepler asp-for,change your input to:

    <input type="text" asp-for="Name" placeholder="Name">
    

    asp-for="Name" will generate to id="Name" name="Name" automatically in html.