Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading