简体   繁体   English

在ASP.NET MVC中实现简单搜索功能

[英]Implementing a Simple Search Function in ASP.NET MVC

I'm trying to implement a basic search page in the web app I'm developing. 我正在尝试在我正在开发的Web应用程序中实现基本搜索页面。 Right now the page looks like this 现在页面看起来像这样

在此输入图像描述

When a user enters a last name, the controller gets called to search the backend Microsoft SQL Server database for all accounts with that Last name 当用户输入姓氏时,控制器将被调用以在后端Microsoft SQL Server数据库中搜索具有该姓氏的所有帐户

Right now the HTML form looks like this 现在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>
}

It's supposed to call this controller 它应该叫这个控制器

[HttpPost]
public void SearchAct()
{
    Console.WriteLine();
}

which will eventually execute the search and then put the results on the page. 最终将执行搜索,然后将结果放在页面上。 However, I can't get the controller to be called. 但是,我无法调用控制器。 I set a break point on the WriteLine so I know its never getting there and I don't know what I'm doing wrong 我在WriteLine上设置了一个断点,所以我知道它永远不会到达那里,我不知道我做错了什么

Add name attribute to your text box. 将名称属性添加到文本框中。 Form collection build based on only name attributes. 仅基于名称属性构建表单集合。

Change button type to submit, then it will post your form to controller. 更改要提交的按钮类型,然后它会将您的表单发布到控制器。

@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>
    }
}

Instead on Console.WriteLine() use ViewBag to send your required data back to view Refer below actions 而是在Console.WriteLine()使用ViewBag将所需数据发送回视图请参阅以下操作

//Get Action for rendering view
public ActionResult SearchAct()
{
    return View();
}


[HttpPost]
public ActionResult SearchAct(string nameToFind)
{
    ViewBag.SearchKey = nameToFind;

    return View();
}

Action parameter name and text box name attribute value must be same otherwise it will null 操作参数名称和文本框名称属性值必须相同,否则将为null

If your form contains multiple text box then read all input values form FromCollection or Request 如果您的表单包含多个文本框, FromCollectionRequest中读取所有输入值

[HttpPost]
public ActionResult SearchAct(FormCollection form)
{
    ViewBag.SearchKey = form["nameToFind"];

    return View();
}

First: To create a form via using razor syntax: 第一:使用razor语法创建表单:

@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post))
{

}

That would generate a form like this: 这将生成这样的表单:

<form method="post" action="/HomeController/SearchAct">

</form>

So, no need to create a nested form inside that. 因此,无需在其中创建嵌套表单。

@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post))
{
    <form> // <- here
        <div>
            Last Name:<br>
            <input type="text" id="nameToFind">

            <input type="button" id="submitId" value="submit" />

        </div>
    </form> // <- here
}

Second: HomeController would match the controller what the full name is HomeControllerController . 第二: HomeController会匹配控制器的全名是HomeControllerController

So, if you want to hit Home controller, remove Controller from HomeController in @using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post)) {} 因此,如果你想点击Home控制器,请在@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post)) {}HomeController中删除Controller @using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post)) {}

Thirth: if you want to catch nameToFind , you can try: Thirth:如果你想抓住nameToFind ,你可以尝试:

[HttpPost]
public void SearchAct(string nameToFind)
{
    Console.WriteLine();
}

Hope this help! 希望这有帮助!

correct your form HomeController should be just Home 纠正你的形式HomeController应该只是Home

@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>
}

controller should accept the input parameter for your filter 控制器应接受过滤器的输入参数

[HttpPost]
public void SearchAct(string nameToFind)
{
    // filter snomething
    Console.WriteLine(nameToFind);
}

first of all we dont need to two form tag and you dont need to add controller suffix. 首先,我们不需要两个表单标签,你不需要添加控制器后缀。 remve that: 请注意:

    @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>
 }

Secondly if you need to search you need to add paramater same as the name of input type text : 其次,如果需要搜索,则需要添加与输入类型文本名称相同的参数:

    [HttpPost]
    public void SearchAct(string nameToFind)
    {
     Console.WriteLine();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM