【问题标题】:I am unable to get the values from a form from cshtml page to my controller我无法从 cshtml 页面的表单中获取值到我的控制器
【发布时间】:2023-02-06 14:20:03
【问题描述】:

将表单提交给代码后,我在控制器中得到空值

<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 + "testing if value is there";
return View();
}

enter image description here

【问题讨论】:

标签: .net forms model-view-controller razor


【解决方案1】:

您应该为您的操作设置 HttpPost 属性。默认情况下,该操作似乎是 get 方法,而不是 post 方法。这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-16
    • 2021-04-01
    • 2020-10-02
    相关资源
    最近更新 更多