如果我将其属性称为HttpPost,则无法调用方法

时间:2017-07-24 16:48:51

标签: asp.net-mvc controller http-post action-filter

我有一个登录页面,用户可以使用登录,我在控制器中将其方法定义为HttpPost,当我尝试从浏览器访问它时,它显示没有找到文件,如果我删除HttpPost属性它命中控制器和返回视图。如果我没有将其类型称为HttpPost,它将表单中的值传递给控制器​​。这是我的Login / SignIn.cshtml代码:

@model BOL.Player_Access
@using (Html.BeginForm("SignIn","Login",FormMethod.Post)){
@Html.AntiForgeryToken()
 <div class="form-horizontal">
    <h4>Sign In</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.PlayerEmail, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PlayerEmail, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PlayerEmail, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="SignIn" class="btn btn-default" />
        </div>
    </div>
</div>

我的LoginController代码在这里

[AllowAnonymous] //This filter removes authorize filter for this controller alone and allow anonyomous request
public class LoginController : Controller
{
    // GET: Login
    public ActionResult Index()
    {
        return View();
    }

   [HttpPost]
    public ActionResult SignIn(Player_Access plyr_obj)
    {
        return View();
    }
}

1 个答案:

答案 0 :(得分:3)

您需要GET和POST代码。

您需要一个GET操作来显示将要POST的表单

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

这将显示SignIn视图。

然后,您需要一个POST操作来从表单中获取值并将它们发送到控制器。

[HttpPost]
public ActionResult SignIn(Player_Access plyr_obj)
{
    //do some work to authenticate the user

    return View();
}