HttpPost没有被触发

时间:2016-08-18 17:40:51

标签: asp.net-mvc-3 razor

我有一个网站,我需要根据用户的角色重定向用户。单击按钮,如果用户是管理员,则重定向到另一个页面;否则重新加载同一页面。单击按钮,无论谁登录,都会加载索引页。在调试中我发现,[HttpPost]属性根本没有被触发。

查看:

@model namespace.ViewModels.LoginVM
@{
    ViewBag.Title = "Login";
}

<h1>User Login</h1>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    <br />
    <div style="background-color: skyblue; width: 50%">
    <div style="padding-left: 1em">
        <div class="display-label" style="font-size: large">
            Enter User Info<br />
            <br />
        </div>
        <div>
            <div class="editor-label">@Html.LabelFor(model => model.empID)</div>
            <div class="editor-field">@Html.TextBoxFor(model => model.empID)
                @Html.ValidationMessageFor(model => model.empID)
            </div>
            <br />
        </div>
        <br />
        <div>
            <input id="submit" type="submit" value="Submit" />
        </div>
    </div>
    </div>
    <br />
}

控制器:

public class LoginController : Controller
{
    [HttpGet]
    public ActionResult Login()
    {           
        return View();
    }

    [HttpPost]
    public ActionResult Login(LoginVM model) 
    {
        MySQL msql = new MySQL();            
        var empID= model.empID
        var role = msql.Select("Select `role` from empDB where `eID` = '" + empID + "'");
        if(role == "admin") 
        { 
            return RedirectToAction("Index","Home"); 
        }
        else 
        {
            return View(); 
        }            
    }
}

1 个答案:

答案 0 :(得分:0)

您的HttpPost操作方法名称为Login。但你的剃刀观点,你使用了索引!

更新Html.BeginForm方法调用以获取正确的操作方法名称和控制器名称值。然后,当您点击提交时,它会将表单数据发布到/Login/Login

@using (Html.BeginForm("Login", "Login", FormMethod.Post))
{

}