1

我正在尝试在带有 HttpPatch 注释的控制器中使用方法。我的问题是,当我触发该方法时,出现 404 错误,找不到资源。当我按下“Patch”按钮而不是 HttpPatch 请求时,我似乎不满足方法需求并发送 HttpPost 请求。如果有人知道如何使用 HttpPatch 注释触发我的 Patch 方法。这是我的控制器:

[HttpPatch]
public ActionResult Patch()
{

     return View();
}

这是我的看法:

@model Practice.Models.PatchModel


<h2>Index</h2>
@using (Html.BeginForm("Patch", "Home"))
{
    <div>
        @Html.Label("Age")
        <div>
            @Html.TextBoxFor(model => model.age)
        </div>
    </div>
    <div>
        @Html.Label("ID")
        <div>
            @Html.TextBoxFor(model => model.id)
        </div>
    </div>
    <input type="submit" value="Patch" />
}

这是我的模型:

namespace Practice.Models
{
    public class PatchModel
    {
        public int age { get; set; }
        public int id { get; set; }
    }
}
4

1 回答 1

1

这是使用 Ajax 解决我的问题的方法:

@model Practice.Models.Account.RegistrationViewModel    
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Login</title>
        <script src="~/scripts/jquery-2.2.1.min.js"></script>
        <script src="~/scripts/jquery.unobtrusive-ajax.min.js"></script>
    </head>
    <body>
        <div>
            @using (Ajax.BeginForm("ProcessLogin", "Account", new AjaxOptions() { HttpMethod = "PATCH" }))
            {
                @Html.LabelFor(x => x.Login)
                @Html.EditorFor(x => x.Login)
                <br />
                @Html.LabelFor(x => x.Password)
                @Html.PasswordFor(x => x.Password)
                <input type="submit" value="Login" />
            }
        </div>
    </body>
    </html>

和控制器中的操作方法:

[HttpPatch]
public ActionResult ProcessLogin()
{
     //some code ...
     return View();
}
于 2016-03-16T09:41:05.197 回答