2014-04-19 12 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"];