英文:
I am unable to get the values from a form from cshtml page to my controller
问题
<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>
这是我的控制器代码
public IActionResult Book(Book obj)
{
string name = obj.Name;
name = name + "测试值是否存在";
return View();
}
英文:
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();
}
答案1
得分: 1
你需要使用 name
属性来绑定表单中的数据,请将您的输入更改为:
<input type="text" id="Name" name="Name" placeholder="Name">
或者如果您正在使用 asp.net core mvc,您可以使用标签助手 asp-for
,将您的输入更改为:
<input type="text" asp-for="Name" placeholder="Name">
asp-for="Name"
将自动在 HTML 中生成 id="Name"
和 name="Name"
。
英文:
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.
答案2
得分: 0
你应该为你的操作设置HttpPost属性。默认情况下,该操作似乎是GET方法,而不是POST方法。像这样做:
[HttpPost]
public IActionResult Book(Book obj)
{
string name = obj.Name;
name = name + "testing if value is there";
return View();
}
英文:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论