簡體   English   中英

C#-ASP.NET MVC-Url.Action獲得HTTP 404

[英]C# - ASP.NET MVC - Url.Action got HTTP 404

出現錯誤

無法找到該資源。 說明:HTTP 404。

我正在嘗試使用剃刀在MVC中執行POST方法。 在我的控制器中:

ServiceListController

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

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

在我中,我使用了Url.Action。 但是,當按下按鈕過濾器時,無法找到請求的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>

嘗試像這樣使用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>}

嘗試這個

ServiceListController

您的方法是針對

[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>}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM