1

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

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
Code Noob
  • 51
  • 6
  • Your fields have IDs but lack names. [Difference between id and name attributes in HTML](https://stackoverflow.com/questions/1397592/difference-between-id-and-name-attributes-in-html). – John Wu Feb 06 '23 at 06:12

2 Answers2

1

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.

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
0

You should set HttpPost attribute for your action. By default, the action seems to be the get method, not the post method. do like this:

**[httpPost]**
public IActionResult Book(Book obj)
{
string name =obj.Name;
name = name + "testing if value is there";
return View();
}
Mahboubeh
  • 1
  • 2