2011-11-17 73 views
8

我是新来的Asp.net MVC,并不知道如何执行搜索。这是我的要求,请告诉我你将如何处理这个问题: -在Asp.net MVC中执行搜索

我需要有文本框,用户可以在其中输入搜索查询或字符串。用户然后点击一个按钮或按下输入提交它。该字符串需要与表格的属性名称匹配。

注意: - 查询数据和获取结果不是这里的要点。我只需要知道如何接受用户输入并将其传递给控制器​​操作或进行进一步处理。只要告诉我你将如何阅读用户输入内容,以及你将它发送到哪里进行搜索。

回答

8

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

让我们来看一个例子。你想制作一个搜索表单。首先,让搜索表单使用HTTP GET方法而不是POST是一种最佳做法,因此搜索结果可以被标记为书签,链接,索引等。我不会使用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”的观点将被渲染,并且该视图的模式将是你的“结果” 。然后你会按照你的意愿显示它。

3

这是最好的方法。

创建一个视图模型

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" /> 
} 

希望这可以帮助

7

与往常一样,在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> 
} 
+0

首先感谢队友。正如你所看到的,在其他答案中@Matteo Mosca提到了HTTP动词的使用。您认为应该使用该视图还是始终遵循视图模型的用法 –

+1

@Pankaj Upadhyay,视图模型和HTTP动词是两个完全不同的概念,它们没有任何共同之处。您应该始终在ASP.NET MVC应用程序中使用视图模型,并且就HTTP动词而言,好吧,因为它是一个Web应用程序,并且基于您已经使用HTTP动词的HTTP协议。如果你愿意,你也可以在HTML表单上使用GET动词。 Html.BeginForm助手有一个重载,它允许你指定这个:@using(Html.BeginForm(null,null,FormMethod.Get)){...}'。然后从您要提交的操作中移除'[HttpPost]'属性。 –

+0

ya ... dats我说的是使用GET动词。你不觉得出于这样的目的,最好是使用它而不是视图模型?因为这种方式不需要为输入查询创建单独的视图模型。 –