【问题标题】:Problems with Html.BeginForm() asp.net MVC 2Html.BeginForm() asp.net MVC 2 的问题
【发布时间】:2014-06-03 13:41:03
【问题描述】:

我的控制器文件夹中有一个 SearchController.cs,它有一个名为 Index 的操作。我的搜索文件夹有一个名为 Index 的视图 以下代码在我的 /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);
    }

以下代码在我的/Home/Index中

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

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

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

【问题讨论】:

  • 你的 get 方法是什么样的?还有你如何区分你的 post 方法被调用。
  • 可能是这个问题,我想我没有get方法。
  • 乍一看,一切似乎都是应有的。我会尝试以下操作:(1)在主页/索引页面上执行 View->Source 并确保正确呈现 &lt;form&gt; 元素; (2) 安装 Fiddler 并在您提交时查看它在哪里发布数据以及它为表单值发布的内容; (3) 使用调试器查看它是否进入了 SearchController 的 Index 操作。
  • 如果你的方法没有被调用,应该有异常。问题是您如何确定索引是否被调用。
  • 也许你有一个嵌套表单?像围绕这个的另一种形式?我已经看到在这种情况下发生了类似的问题。 (嵌套形式无效)

标签: c# asp.net-mvc asp.net-mvc-2


【解决方案1】:

我也建议尝试使用它。

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

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

【讨论】:

  • 我添加了 FormMethod.Post 但什么也没有
  • 发布是默认选项。
【解决方案2】:

在你的行动中试试这个:

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

【讨论】:

    猜你喜欢
    • 2010-11-03
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多