2014-04-19 96 views
0

我在我的控制器文件夾中有一個SearchController.cs,它有一個名爲Index的Action。我的搜索文件夾中有一個名爲Index 下面的代碼是針對我的/控制器/ SearchControllerHtml.BeginForm問題()asp.net MVC 2

private TEAM2BooksDBEntities _db = new TEAM2BooksDBEntities(); 
    [HttpPost] 
    public ActionResult Index(string SearchFor) 
    { 
     var query = _db.Books.Where(em => em.title.Contains(SearchFor)).ToList(); 
     return View(query); 
    } 

下面的代碼是在我的/首頁/索引

<% using(Html.BeginForm("Index","Search")){ %> 
    <%= Html.TextBox("SearchFor") %> 

    <input type="submit" value="Submit" /> 
    <% }%> 

但無論我做什麼時我點擊提交按鈕,它只是重新加載當前頁面。我希望它將「SearchFor」框的內容作爲參數發送到Search控制器中的Index操作。我怎樣才能解決這個問題?

+0

如何你的get方法是什麼樣子?另外你如何區分你的post方法被調用。 – dotnetstep

+0

也許是這個問題,我不認爲我有一個get方法。 – Dominic

+0

乍一看,一切似乎都應該如此。我會嘗試以下方法:(1)在Home/Index頁面上執行View-> Source,並確保'

'元素被正確渲染; (2)安裝Fiddler並查看發佈數據的位置以及提交時發佈的表單值; (3)使用調試器查看它是否進入了SearchController的'Index'操作。 –

回答

0

我也建議嘗試使用它。

<% using(Html.BeginForm("Index","Search",FormMethod.Post)){ %> 
<%= Html.TextBox("SearchFor") %> 

<input type="submit" value="Submit" /> 
<% }%> 
+0

我添加了FormMethod.Post,但沒有任何 – Dominic

+0

Post是默認選項。 –

0

試試這個你的行動中:

string SearchFor= Request.Form["SearchFor"];