【问题标题】:ASP.NET MVC 3 - two forms, two ActionResult but one Controller?ASP.NET MVC 3 - 两种形式,两种 ActionResult 但一种控制器?
【发布时间】:2012-02-25 19:48:17
【问题描述】:

我有一个新的 asp.net mvc 3 项目,结构如下:

观看次数: /Home/Index.cshtml

@{
    Html.BeginForm("Index", "Home", "POST");
}
<input id="name" type="text" />
<input id="submit_1" type="submit" value="submit" />
@{
    Html.EndForm();
}

@{
    Html.BeginForm("FindTeacher", "Home", "POST");
}
<input id="name" type="text" />
<input id="submit_2" type="submit" value="submit" />
@{
    Html.EndForm();
}

控制器: /Controllers/HomeController.cs

public class HomeController : Controller
{

    [HttpPost]
    public ActionResult Index(string name)
    {
        //call the model FindStudent() and set the ViewData
        return View();
    }

    [HttpPost]
    public ActionResult FindTeacher(string name)
    {
        //Call the FindTeacher () and set the ViewData
        return View();
    }
}

submit_1是有效的,因为它找到了Index的ActionResult,但是,当我点击submit_2时,它说找不到FindTeacher Controller。

那我该怎么办?

【问题讨论】:

  • @using(Html.BeginForm(...)) { ... }

标签: asp.net-mvc


【解决方案1】:

试试这个

[HttpPost] 
public ActionResult FindTeacher(string name) 
{ 
  // do updates 
  return RedirectToAction("Index"); or 
  return Index(); 
} 

【讨论】:

  • 我在返回 RedirectToAction("Index") 时设置了一个断点,它发生了。但我无法获取 [name] 参数,它变为 null。
  • 我让它工作,输入应该有名称属性,而不仅仅是像 webform 这样的 [id]。
【解决方案2】:

"POST" 错误;应该是FormMethods.Post

因此,您的表单实际上是在提交 GET 请求。

Index 有效,因为您有一个不同的操作来响应对/Index 的 GET 请求。
FindTeacher 失败,因为该操作没有 GET。

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2015-11-14
    • 2011-02-03
    • 1970-01-01
    相关资源
    最近更新 更多