u013180863
zs过客
采纳率33.3%
2017-06-01 00:03 阅读 761

asp.net mvc 使用连接登录时如何禁止put属性

50

代码如下:

 <body>
    <div class="login">   
        @using (Html.BeginForm("doLogin", "Login", null, FormMethod.Post, new { id = "mainForm" }))  
        {  
            <ul>  
                <li><span>帐 号:</span>@Html.TextBoxFor(model => model.Name, new { @name = "user", @id = "txtacc" })</li>  
                <li><span>密 码:</span>@Html.PasswordFor(model => model.Password, new { @name = "pasword", @id = "txtpwd" })</li>  
                <li><span>邮 箱:</span>@Html.TextBoxFor(model => model.Mail, new { @name = "name", @id = "txtmail" })</li>
                <li><a href="javascript:check(); ">登 录</a></li>  
            </ul>  
        }  
    </div>  
</body>
</html>
<script type="text/javascript">

    function check() {

        var acc = document.getElementById('txtacc').value;
        var pwd = document.getElementById('txtpwd').value;
        var com = document.getElementById('txtmail').value;

        if ("" == acc) {
            alert('账号不能为空!');
        }
        else if ("" == pwd) {
            alert('密码不能为空!');
        } else if ("" == com) {
            alert('密码不能为空!');
        }

        else {
            $('#mainForm').submit();
        }


    }

</script> 

在control中代码如下:

 [HttpPost]
        public ActionResult doLogin(Employee e)
        {
                EmployeeBusinessLayer ebl = new EmployeeBusinessLayer();
                var user = ebl.Get(e.Name, e.Mail, e.Password);
                bool IsAdmin = false;
                if (user != null)
                {
                    IsAdmin = true;
                    ViewBag.Message = "success!";
                    FormsAuthentication.SetAuthCookie(user.Name, false);
                    Session["IsAdmin"] = IsAdmin;
                    return RedirectToAction("Index", "Home", e);
                }
                else
                {
                    IsAdmin = false;
                    ViewBag.Message = "Failed!";
                    Session["IsAdmin"] = IsAdmin;
                    return View("Login", e);
                }

看代码都是post请求,但是在访问时会出现如下图所示的情况,求问,如何更改使其成为post请求。
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • showbo GoCityPass新加坡曼谷通票 2017-06-01 01:58

    mvc没用过,你应该是用错了,生成的表单客户端html代码并不是post的,变get了。用js改为post

      $('#mainForm').submit();
     ==》
      $('#mainForm').attr('method','post').submit();
    
    点赞 1 评论 复制链接分享
  • qq_36279445 Martin_Yelvin 2017-06-01 01:01

    FormMethod.Post 登录试试 type ='submit'

    点赞 评论 复制链接分享
  • wodewojueding wodewojueding 2017-06-01 08:18

    submit 没有问题,你看看是不是你最后返回的View是Get请求,所以参数才会出现在地址栏中;

    点赞 评论 复制链接分享

相关推荐