[英]How can I make my controller catch the data submitted by a form from one of his views in ASP.NET MVC
So basically I want to make search functionality in ASP.NET MVC.所以基本上我想在 ASP.NET MVC 中创建搜索功能。 I am currently trying to retrieve data from a form as a string and print it to ensure that it gets the correct information.
我目前正在尝试从表单中检索数据作为字符串并打印它以确保它获得正确的信息。 My view and controller are:
我的观点和控制器是:
View:看法:
<div style="text-align: center;">
<form method="post" asp-controller="MainPage" asp-action="Search" id="form1">
<label for="licensePlate">Enter license plate:</label>
<input type="text" placeholder="License Plate" name="licensePlate" id="licensePlate" />
<button type="submit">Search</button>
</form>
</div>
Controller:控制器:
public class MainPageController : Controller
{
private readonly ILogger<MainPageController> _logger;
private readonly UnblockMeContext _dbContext;
public MainPageController(ILogger<MainPageController> logger,UnblockMeContext appData)
{
_logger = logger;
_dbContext = appData;
}
public IActionResult Index()
{
var users = _dbContext.Users.ToList();
return View(users);
}
public IActionResult Search(string result)
{
return Content(result);
}
}
When I press on search button to submit the form a blank page appears but I want to print out the string that was submitted.当我按下搜索按钮提交表单时,会出现一个空白页,但我想打印出提交的字符串。
How should I do this ?我该怎么做?
You need to change the controller search action method parameter name to match with the FORM NAME.您需要更改控制器搜索操作方法参数名称以与 FORM NAME 匹配。
public IActionResult Search(string licensePlate = "")
{
return Content(licensePlate);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.