在Asp.net MVC中执行搜索

9
我是Asp.net MVC的新手,不知道如何进行搜索。以下是我的需求,请告诉我你将如何处理:
我需要一个文本框,用户可以在其中输入搜索查询或字符串。然后,用户点击按钮或按下回车键提交。字符串需要与表的属性名称匹配。
注意:这里并不是查询数据和获取结果的重点。我只需要知道你如何读取用户输入,并将其传递给控制器操作或其他进一步处理的位置。请告诉我你将如何读取用户输入以及将其发送到哪里进行搜索。
3个回答

11

Asp.Net MVC使用标准的HTTP动词。对于HTML部分,它是一个指向URL的普通HTML表单。在服务器端,该URL将被路由到处理输入并执行所需操作的控制器/操作。

让我们来举个例子。您想创建一个搜索表单。首先,最好使用HTTP GET方法来使用搜索表单,以便搜索结果可以被收藏夹、链接、索引等。我不会使用Html.BeginForm帮助方法来使事情更加清晰。

<form method="get" action="@Url.Action("MyAction", "MyController")">
<label for="search">Search</label>
<input type="text" name="search" id="search" />
<button type="submit">Perform search</button>
</form>

这就是你所需要的所有HTML代码。现在你将拥有一个名为“MyController”的控制器,其方法将类似于以下代码:

[HttpGet]
public ActionResult MyAction(string search)
{
//do whatever you need with the parameter, 
//like using it as parameter in Linq to Entities or Linq to Sql, etc. 
//Suppose your search result will be put in variable "result".
ViewData.Model = result;
return View();
}

现在将呈现名为"MyAction"的视图,并且该视图的模型将成为您的“result”。然后您可以根据自己的愿望展示它。


8
作为ASP.NET MVC应用程序中的常规流程,您需要首先定义一个视图模型,以表达视图的结构和要求。到目前为止,您已经谈论了包含搜索输入框的表单:
public class SearchViewModel
{
    [DisplayName("search query *")]
    [Required]
    public string Query { get; set; }
}

然后你需要编写一个控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new SearchViewModel());
    }

    [HttpPost]
    public ActionResult Index(SearchViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // There was a validation error => redisplay the view so 
            // that the user can fix it
            return View(model);
        }

        // At this stage we know that the model is valid. The model.Query
        // property will contain whatever value the user entered in the input
        // So here you could search your datastore and return the results

        // You haven't explained under what form you need the results so 
        // depending on that you could add other property to the view model
        // which will store those results and populate it here

        return View(model);
    }
}

最后是一个视图:
@model SearchViewModel

@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.Query)
    @Html.EditorFor(x => x.Query)
    @Html.ValidationMessageFor(x => x.Query)
    <button type="submit">Search</button>
}

首先感谢你。正如你所看到的,在另一个答案中@Matteo Mosca提到了HTTP动词的使用。你认为应该使用它还是始终遵循视图模型的用法? - Pankaj Upadhyay
1
@Pankaj Upadhyay,视图模型和HTTP动词是两个完全不同的概念,彼此之间没有任何关联。在ASP.NET MVC应用程序中,您应该始终使用视图模型;至于HTTP动词,既然它是一个基于HTTP协议的Web应用程序,您已经在使用HTTP动词了。如果您愿意,您也可以在HTML表单上使用GET动词。Html.BeginForm帮助器有一个重载,允许您指定它:@using (Html.BeginForm(null, null, FormMethod.Get)) { ... }。然后从您提交到的操作中删除[HttpPost]属性。 - Darin Dimitrov
那就是我所说的使用GET动词。你不认为对于这样的目的,使用它要比ViewModel更好吗?因为这样就不需要为仅用于获取输入查询的目的创建单独的ViewModel了。 - Pankaj Upadhyay
3
@Pankaj Upadhyay,您仍然可以在表单上使用GET动词来使用视图模型。我认为您应该始终使用视图模型。 - Darin Dimitrov

3

This is the best way to do it.

Create a ViewModel

public class SearchViewModel
{
    public string Query { get; set; }
}

创建控制器

    public class SearchController : Controller
    {
        [HttpPost]
        public ActionResult Search(SearchViewModel model)
        {
            // perform search based on model.Query

            // return a View with your Data.
        }
    }

创建视图

// in your view
@using (Html.BeginForm("Search", "SearchController"))
{
    @Html.TextBox("Query")
    <input type="submit" value="search" />
}

希望这可以帮到您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接