2011-11-17 74 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動詞。你不覺得出於這樣的目的,最好是使用它而不是視圖模型?因爲這種方式不需要爲輸入查詢創建單獨的視圖模型。 –