【问题标题】:C# - ASP.NET MVC - Url.Action got HTTP 404C# - ASP.NET MVC - Url.Action 得到 HTTP 404
【发布时间】:2017-11-10 18:42:57
【问题描述】:

有一个错误

找不到资源。 说明:HTTP 404。

我正在尝试使用 razor 在我的 MVC 中执行 POST 方法。在我的控制器中:

ServiceListController

public ActionResult Index()
{ 
    return View();
}

[HttpPost]
private ActionResult GetData(string txtTech, string txtSerial, string txtJobNo)
{ 
    return View();
}

我使用了 Url.Action。但是当按下按钮过滤器时,Requested URL: /ServiceList/GetData 资源找不到。我需要添加到路线或其他东西吗?

<form action="@Url.Action("GetData", "ServiceList")" method="post">
    @Html.AntiForgeryToken()

    <table style="width: 400px">
            <td>
                @Html.Label("Technician", new { style = "width: 50px;" })
                <input type="text" id="txtTech" name="txtTech" class="form-control" style="width: 200px" />
            </td>
            <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
            <td>
                @Html.Label("Serial", new { style = "width: 50px;" })
                <input type="text" id="txtSerial" name="txtSerial" class="form-control" style="width: 200px" />
            </td>
            <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
            <td>
                @Html.Label("Job No", new { style = "width: 50px;" })
                <input type="text" id="txtJobNo" name="txtJobNo" class="form-control" style="width: 200px" />
            </td>
            <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
            <td>
                @Html.Label("a", new { style = "visibility: hidden;" })
                @Html.DropDownList("txtStatus", new SelectList(Enum.GetValues(typeof(wmssoft_srm.Models.Status))), "Status", new { @class = "form-control", style = "width: 200px;" })
            </td>
        </tr>
        ....
        <tr>
            <td>
                <button class="btn btn-info" type="submit" id="btFilter">Clear Filter</button>
            </td>
        </tr>
    </table>
</form>

【问题讨论】:

  • 因为你[HttpPost]方法是private - 让它public
  • 将 GetData 方法私有 sccess 说明符更改为 public。
  • @StephenMuecke 非常感谢!没注意。
  • @AjasAju 你确定吗?
  • @SeM.Nop.i 编辑了我的答案。这是一个错误的想法。

标签: c# asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

尝试像这样使用这个 html.beginform 并公开发布方法

 @using (Html.BeginForm("GetData", "ServiceList", FormMethod.Post))
{ @Html.AntiForgeryToken()

<table style="width: 400px">
        <td>
            @Html.Label("Technician", new { style = "width: 50px;" })
            <input type="text" id="txtTech" name="txtTech" class="form-control" style="width: 200px" />
        </td>
        <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
        <td>
            @Html.Label("Serial", new { style = "width: 50px;" })
            <input type="text" id="txtSerial" name="txtSerial" class="form-control" style="width: 200px" />
        </td>
        <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
        <td>
            @Html.Label("Job No", new { style = "width: 50px;" })
            <input type="text" id="txtJobNo" name="txtJobNo" class="form-control" style="width: 200px" />
        </td>
        <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
        <td>
            @Html.Label("a", new { style = "visibility: hidden;" })
            @Html.DropDownList("txtStatus", new SelectList(Enum.GetValues(typeof(wmssoft_srm.Models.Status))), "Status", new { @class = "form-control", style = "width: 200px;" })
        </td>
    </tr>
    ....
    <tr>
        <td>
            <button class="btn btn-info" type="submit" id="btFilter">Clear Filter</button>
        </td>
    </tr>
</table>}

【讨论】:

    【解决方案2】:

    试试这个

    服务列表控制器

    您的方法已更新

    [HttpPost]
    [ValidateAntiForgeryToken]
    Public ActionResult GetData(string txtTech, string txtSerial, string txtJobNo)
    { 
        return View();
    }
    

    更新视图操作 你可以尝试两种方法

    1)

    <form action="@Url.Action("GetData", "ServiceList")" method="post">
        @Html.AntiForgeryToken()
    
        <table style="width: 400px">
                <td>
                    @Html.Label("Technician", new { style = "width: 50px;" })
                    <input type="text" id="txtTech" name="txtTech" class="form-control" style="width: 200px" />
                </td>
                <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
                <td>
                    @Html.Label("Serial", new { style = "width: 50px;" })
                    <input type="text" id="txtSerial" name="txtSerial" class="form-control" style="width: 200px" />
                </td>
                <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
                <td>
                    @Html.Label("Job No", new { style = "width: 50px;" })
                    <input type="text" id="txtJobNo" name="txtJobNo" class="form-control" style="width: 200px" />
                </td>
                <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
                <td>
                    @Html.Label("a", new { style = "visibility: hidden;" })
                    @Html.DropDownList("txtStatus", new SelectList(Enum.GetValues(typeof(wmssoft_srm.Models.Status))), "Status", new { @class = "form-control", style = "width: 200px;" })
                </td>
            </tr>
            ....
            <tr>
                <td>
                    <button class="btn btn-info" type="submit" id="btFilter">Clear Filter</button>
                </td>
            </tr>
        </table>
    </form>
    

    2)

    @using (Html.BeginForm("GetData", "ServiceList", FormMethod.Post))
    { 
      @Html.AntiForgeryToken()
    
    <table style="width: 400px">
            <td>
                @Html.Label("Technician", new { style = "width: 50px;" })
                <input type="text" id="txtTech" name="txtTech" class="form-control" style="width: 200px" />
            </td>
            <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
            <td>
                @Html.Label("Serial", new { style = "width: 50px;" })
                <input type="text" id="txtSerial" name="txtSerial" class="form-control" style="width: 200px" />
            </td>
            <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
            <td>
                @Html.Label("Job No", new { style = "width: 50px;" })
                <input type="text" id="txtJobNo" name="txtJobNo" class="form-control" style="width: 200px" />
            </td>
            <td>@Html.Label("a", new { style = "visibility: hidden;" })</td>
            <td>
                @Html.Label("a", new { style = "visibility: hidden;" })
                @Html.DropDownList("txtStatus", new SelectList(Enum.GetValues(typeof(wmssoft_srm.Models.Status))), "Status", new { @class = "form-control", style = "width: 200px;" })
            </td>
        </tr>
        ....
        <tr>
            <td>
                <button class="btn btn-info" type="submit" id="btFilter">Clear Filter</button>
            </td>
        </tr>
    </table>}
    

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 1970-01-01
      • 2015-02-11
      • 2021-08-06
      • 2020-12-29
      • 2010-12-18
      • 2013-02-23
      • 2011-09-18
      • 2019-09-20
      相关资源
      最近更新 更多