C# 在ASP.NET MVC中实现简单的搜索功能
我正试图在我正在开发的web应用程序中实现一个基本的搜索页面。现在页面看起来像这样C# 在ASP.NET MVC中实现简单的搜索功能,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正试图在我正在开发的web应用程序中实现一个基本的搜索页面。现在页面看起来像这样 @using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post)) { <form> <div> Last Name:<br> <input type="text" id="nameToFind">
@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post))
{
<form>
<div>
Last Name:<br>
<input type="text" id="nameToFind">
<input type="button" id="submitId" value="submit" />
</div>
</form>
}
当用户输入姓氏时,将调用控制器在后端Microsoft SQL Server数据库中搜索具有该姓氏的所有帐户
现在HTML表单如下所示
@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post))
{
<form>
<div>
Last Name:<br>
<input type="text" id="nameToFind">
<input type="button" id="submitId" value="submit" />
</div>
</form>
}
最终将执行搜索,然后将结果放在页面上。但是,我无法调用控制器。我在WriteLine上设置了一个断点,因此我知道它永远不会到达那里,我也不知道我做错了什么
@using (Html.BeginForm("SearchAct", "Home", FormMethod.Post))
{
<form>
<div>
Last Name:<br>
<input type="text" id="nameToFind">
<input type="button" id="submitId" value="submit" />
</div>
</form>
}
第一:要使用razor语法创建表单,请执行以下操作:
@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post))
{
}
这将生成如下表单:
<form method="post" action="/HomeController/SearchAct">
</form>
希望这有帮助 将name属性添加到文本框中。仅基于名称属性生成表单集合 将按钮类型更改为提交,然后它会将您的表单发布到控制器
@using (Html.BeginForm("SearchAct", "Home", FormMethod.Post))
{
<div>
Last Name:<br>
<input type="text" id="nameToFind" name="nameToFind">
<input type="submit" id="submitId" value="submit" />
</div>
}
@{
if(ViewBag.SearchKey != null)
{
<span>
Search Key: @ViewBag.SearchKey
</span>
}
}
操作参数名称和文本框名称属性值必须相同,否则将null
如果表单包含多个文本框,则从集合或请求中读取所有输入值
[HttpPost]
public ActionResult SearchAct(FormCollection form)
{
ViewBag.SearchKey = form["nameToFind"];
return View();
}
首先,我们不需要两个表单标签,也不需要添加控制器后缀。记住:
@using (Html.BeginForm("SearchAct", "Home", FormMethod.Post))
{
<div>
Last Name:<br>
<input type="text" id="nameToFind" name="nameToFind">
<input type="button" id="submitId" value="submit" />
</div>
}
可能是
FormMethod.Get
而不是Post。您需要在方法中绑定一个参数,比如说public ActionResult SearchAct(string searchText)
,并且输入必须有一个匹配的name
属性-name=“searchText”
(但是如果您使用视图模型并使用@Html.TextBoxFor(m=m.searchText)强绑定到它,您会发现这要容易得多)
。你的方法需要是ActionResult
并返回一些东西。假设它是HomeController
,而不是HomeControllerController
——那么BeginForm()
的第二个参数是“Home”
@using (Html.BeginForm("SearchAct", "Home", FormMethod.Post))
{
<div>
Last Name:<br>
<input type="text" id="nameToFind" name="nameToFind">
<input type="submit" id="submitId" value="submit" />
</div>
}
@{
if(ViewBag.SearchKey != null)
{
<span>
Search Key: @ViewBag.SearchKey
</span>
}
}
//Get Action for rendering view
public ActionResult SearchAct()
{
return View();
}
[HttpPost]
public ActionResult SearchAct(string nameToFind)
{
ViewBag.SearchKey = nameToFind;
return View();
}
[HttpPost]
public ActionResult SearchAct(FormCollection form)
{
ViewBag.SearchKey = form["nameToFind"];
return View();
}
@using (Html.BeginForm("SearchAct", "Home", FormMethod.Post))
{
<div>
Last Name:<br>
<input type="text" id="nameToFind" name="nameToFind">
<input type="button" id="submitId" value="submit" />
</div>
}
[HttpPost]
public void SearchAct(string nameToFind)
{
Console.WriteLine();
}