Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC4自定义登录控制器_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# MVC4自定义登录控制器

C# MVC4自定义登录控制器,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我需要一些MVC方面的帮助。我正在尝试更新我继承的一些遗留代码,我绝对是一个有网络内容的n00b。同样的代码也适用于已安装版本的站点,但在此处无法正常工作 Index.chtml文件是: @model QspAudits.Models.LoginModel @{ Layout = null; // Unimportant viewbag stuff. Can post if needed. } <!DOCTYPE html> <html> &l

我需要一些MVC方面的帮助。我正在尝试更新我继承的一些遗留代码,我绝对是一个有网络内容的n00b。同样的代码也适用于已安装版本的站点,但在此处无法正常工作

Index.chtml文件是:

@model QspAudits.Models.LoginModel
@{
    Layout = null;
    // Unimportant viewbag stuff.  Can post if needed.
}

<!DOCTYPE html>
<html>
    <head>
        <title>@ViewBag.Site | @ViewBag.Title</title>
        @Styles.Render("http://hotline.rimg.net/qspaudits/Content/css/qsp.css")
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function submitenter(myfield, e) {
                var keycode;
                if (window.event) keycode = window.event.keyCode;
                else if (e) keycode = e.which;
                else return true;
                if (keycode == 13) {
                    $("#FormLogin").submit();
                    return false;
                }
                else
                    return true;
            }
            $(document).ready(function () {
                $('.login_btn').click(function () {
                    $(this).closest('form').submit();
                });
            });
        </script>
    </head>
<body id="@ViewBag.BodyID" class="@ViewBag.BodyClass">
    @using (Html.BeginForm("Index", "Login", FormMethod.Post, new {id = "FormLogin"}))
    {
        <div id="loginBox" class="login">
            <div class="loginForm">
                <img alt="" src="@Url.Content("~/Images/samsclub_medlogo.png")" class="identity"/>
                <h2>@ViewBag.Site</h2>
                <label>Login</label>
                @Html.TextBoxFor(x => x.username, new { @class = "TextBox", @autofocus=""})
                <label>Password</label>
                @Html.PasswordFor(x => x.password, new { @class = "PasswordBox", @onkeypress = "return submitenter(this,event)" })
                <div id="login_btn" class="login_btn"></div>     <span>@Model.message</span>
            </div>
        </div>
    }
</body>
</html>
当我运行代码通过VS2012进行测试时,唯一被击中的断点是[HttpGet]块。奇怪的是,它两次命中[HttpGet]块断点。[HttpPost]块从未被触及


这段代码是在live server上运行的,所以一定是我遗漏了一些愚蠢的简单代码。

这有什么意义。POST方法作为返回RedirectToAction和ajax调用不会重定向。只需删除所有无意义的脚本,用于执行正常的submitOkay,因此,这是一个很大的冗长,但我不能说它与任何事情都有任何关系。更改/删除ajax内容将如何影响从未被调用的方法的返回结果?我只是指出,脚本只是不必要的。为什么不直接删除它并使用submit按钮呢?所以,没有任何线索说明为什么代码没有真正做到它应该做的,只是对以前的devs编码技术的一个评论?
namespace QspAudits.Controllers
{
    public class LoginController : Controller
    {
        private QspAuditsEntities db = new QspAuditsEntities();
        private BT8_GCEntities bt_db = new BT8_GCEntities();

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Index()
        {
            ViewBag.bodyID = "login";
            LoginModel log = new LoginModel();
            log.message = "";
            return View(log);
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Index(LoginModel formResult)
        {
            UserLogin_Result log = db.UserLogin(formResult.username).FirstOrDefault();
            if (log != null && formResult.password != null)
            {
                int pass;
                if (Int32.TryParse(log.password.ToString(), out pass) == true || log.validation == 1)
                {
                    int dbPass = (log.validation == 3 ? pass : Common.MD5Hash(formResult.password));
                    if (dbPass == (int)log.password) //use real valid code
                    {
                        FormsAuthentication.SetAuthCookie(log.user_id.ToString() + "|" + log.full_name + "|" + (log.validation == 1 ? "1" : "3"), false);
                        return RedirectToAction("Index", "Home");
                    }
                }
            }
            formResult.password = "";
            formResult.message = "Incorrect Username/Password";
            return View(formResult);
        }

        public ActionResult SignOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Login");
        }
    }
}