MVC表单不是控制器方法

rig*_*onk -1 c# asp.net-mvc asp.net-mvc-4

我是mvc的新手,所以我仍在努力解决一些问题.我有一个非常简单的形式和控制器.控制器:

    public class InquiryController : BaseController {

    public ActionResult Index() {

        return View();
    }

    public ActionResult Search(int Id) {

         int newid = Id;

        return View();

    }
}
Run Code Online (Sandbox Code Playgroud)

和形式:

@using (Html.BeginForm("Index", "Results", FormMethod.Post))
{
    @Html.TextBox("id", null, new { type = "Search", autofocus = "true", style = "width: 200px" })
    <input type="submit" value="Search" />
}
Run Code Online (Sandbox Code Playgroud)

发生的事情是,当我在表单中输入数字时,我希望将该数字传递给控制器​​中的"搜索"功能.我不明白如何让表单调用控制器并在该控制器内进行处理.相反,它会查找"结果"页面时出错,我理解它来自BeginForm方法.所以从本质上讲,我如何让我的表单将输入搜索框的数字传递给我的控制器功能?

小智 6

在前两个参数中指定要进入的操作和控制器Html.BeginForm.

@using (Html.BeginForm("Search", "Inquiry", FormMethod.Post))
{
    @Html.TextBox("id", null, new { type = "Search", autofocus = "true", placeholder = "Harvest Tag, 6 Digit Tag No.", style = "width: 200px" })
    <input type="submit" value="Search" />
}
Run Code Online (Sandbox Code Playgroud)

里面的动作,您可以访问像你正在做的Id但你也可以得到它FormCollection["id"]Request["id"]作为原始字符串和验证,以避免越来越解析错误的情况下,在用户提交无效INT.