从布局调用控制器

时间:2013-07-25 10:55:14

标签: asp.net-mvc-4 routes

我是MVC的新手。

我的布局页面中有以下内容:

<div id="RunValidation">
        @using (Html.BeginForm("RunValidation", "Home"))
        {
            <table>
                <tr>
                    <td>
                        <input type="checkbox" name="chkFindDuplicates" value="chkFindDuplicates">Find Cards
                    </td>
                </tr>

                <tr>
                    <td>
                        <input type="checkbox" name="chkFindDuplicates" value="chkFindDuplicates">Find Duplicates
                    </td>
                </tr>

                <tr>
                    <td>
                        <input type="checkbox" name="chkFindSuspectVoucherAllocation" value="chkFindSuspectVoucherAllocation">Find Suspect Voucher Allocataion
                    </td>
                </tr>


                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="SubmitValidation" /></td>
                </tr>
            </table>
        }
    </div>

我的HomeController中有以下Action:

public ActionResult RunValidaton()
    {
        //determine which validations checks to run
        int x = 0;


        //call into middle tier to execute the validation

        return View();

    }

我的路线配置如下:

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

当我点击我的html表单上的提交按钮时,我希望在int x = 0上点击一个断点;在控制器中的行,但我得到404错误。

来自Fiddler我可以在Post标题中看到以下内容:

POST /Home/RunValidation HTTP/1.1

我无法理解为什么请求没有被路由到控制器?

2 个答案:

答案 0 :(得分:2)

试试这个,

请更改您的操作方法名称。见下面的代码

<div id="RunValidation">
        @using (Html.BeginForm("Jeet", "Home", FormMethod.Post))
        {
            <table>
                <tr>
                    <td>
                        <input type="checkbox" name="chkFindDuplicates" value="chkFindDuplicates">Find Cards
                    </td>
                </tr>

                <tr>
                    <td>
                        <input type="checkbox" name="chkFindDuplicates" value="chkFindDuplicates">Find Duplicates
                    </td>
                </tr>

                <tr>
                    <td>
                        <input type="checkbox" name="chkFindSuspectVoucherAllocation" value="chkFindSuspectVoucherAllocation">Find Suspect Voucher Allocataion
                    </td>
                </tr>


                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="SubmitValidation" /></td>
                </tr>
            </table>
        }
    </div> 

    [HttpPost]
        public ActionResult Jeet()
        {

            //determine which validations checks to run
            int x = 0;


            //call into middle tier to execute the validation

            return View();

        }


 routes.MapRoute(
            name: "DefaultRunValidation",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Dashboard", action = "Jeet", id = UrlParameter.Optional }
        );

答案 1 :(得分:0)

尝试使用http方法注释和操作参数创建Action。

[HttpPost]
public ActionResult RunValidaton(FormCollection form) //your model here would be better.
    {
        //determine which validations checks to run
        int x = 0;


        //call into middle tier to execute the validation

        return View();

    }