Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Html.BeginForm()asp.net MVC 2存在问题_C#_Asp.net Mvc_Asp.net Mvc 2 - Fatal编程技术网

C# Html.BeginForm()asp.net MVC 2存在问题

C# Html.BeginForm()asp.net MVC 2存在问题,c#,asp.net-mvc,asp.net-mvc-2,C#,Asp.net Mvc,Asp.net Mvc 2,我的控制器文件夹中有一个SearchController.cs,它有一个名为Index的操作。我的搜索文件夹有一个名为Index的视图 以下代码位于my/Controller/SearchController中 private TEAM2BooksDBEntities _db = new TEAM2BooksDBEntities(); [HttpPost] public ActionResult Index(string SearchFor) {

我的控制器文件夹中有一个SearchController.cs,它有一个名为Index的操作。我的搜索文件夹有一个名为Index的视图 以下代码位于my/Controller/SearchController中

    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);
    }
以下代码位于my/Home/Index中

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

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


但无论我在点击提交按钮时做什么,它都会重新加载当前页面。我希望它将“SearchFor”框的内容作为参数发送到搜索控制器中的索引操作。如何修复此问题?

我还建议尝试使用此功能

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

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

在您的操作中尝试以下操作:

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

你的get方法看起来怎么样?还有你是如何区分你的post方法被调用的。也许这就是问题所在,我认为我没有get方法。乍一看,一切都是应该的。我将尝试以下操作:(1)在主页/索引页上执行一个View->Source,并确保
元素被正确呈现;(2) 安装Fiddler,查看它在哪里发布数据,以及在提交时发布的表单值;(3) 使用调试器查看它是否进入SearchController的
索引
操作。如果未调用方法,则应存在异常。问题是您如何识别索引是否被调用。也许您有一个嵌套表单?像这张桌子周围的另一张桌子吗?我在这个场景中看到过类似的问题。(嵌套表单无效)我添加了FormMethod.Post,但默认选项为nothingPost。