• asp.net mvc利用Json验证数据和导向页面,解决重复提交问题


    //HTML代码
    <script src="http://www.cnblogs.com/Repository/JavaScript/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <table cellspacing="0" cellpadding="0" width="100%" border="0" height="143" id="table212">
    <tr>
    <td width="13%" height="38">
    <span class="login_txt">用户名:</span>
    </td>
    <td height="38" colspan="2">
    <input type="text" name="username" id="username" maxlength="15" class="textStyle" />
    </td>
    </tr>
    <tr>
    <td width="13%" height="35">
    <span class="login_txt">&nbsp;&nbsp;&nbsp;&nbsp;码:</span>
    </td>
    <td height="35" colspan="2">
    <input type="password" maxlength="15" id="password" name="password" class="textStyle" />
    </td>
    </tr>
    <tr>
    <td>
    &nbsp;
    </td>
    <td style=" 155px; font-weight: bold; color: Red; font-size: 12px">
    <div id="msg">
    </div>
    </td>
    <td align="left">
    <input id="submitbtn" name="submitbtn" type="button" class="jhp" value="&nbsp;登 录&nbsp;"
    title
    ="登录" />
    <script type="text/javascript">
    $(
    function () {
    $(
    "#submitbtn").click(function () {
    var username = document.getElementById("username").value;
    var userpwd = document.getElementById("password").value;
    var msg = document.getElementById("msg");
    msg.innerHTML
    = "";
    if (!username) {
    msg.innerHTML
    = "请输入用户名";
    return false;
    }

    if (!userpwd) {
    msg.innerHTML
    = "请输入密码";
    return false;
    }
    var url = '../Login/LoginVar.aspx?loginid=' + username + '&loginpwd=' + userpwd;
    $.get(url, { date:
    new Date().getTime() }, function (data) {
    if (data) {
    if (data.url) {
    window.location
    = data.url;
    }
    else {
    msg.innerHTML
    = data;
    }
    }
    },
    "json");
    });
    });

    </script>
    </td>
    </tr>
    </table>

      

    // 后台Action代码

    /// <summary>
    /// 登陆验证
    /// </summary>
    /// <param name="fc"></param>
    public ActionResult LoginVar(string username,string userpwd)
    {

    try
    {
    ZHUser user
    = userRepository.GetUser_username_password(username, Tools.GetMd5By16(userpwd));
    if (user == null)
    {
    var json
    = "账号或密码不正确";
    return Json(json, JsonRequestBehavior.AllowGet);
    }
    else
    {
    FormsAuthentication.SetAuthCookie(username,
    false);
    HttpCookie userCookie
    = new HttpCookie("username", user.UserName);
    HttpCookie userid
    = new HttpCookie("userid", user.Id.ToString());
    Response.Cookies.Add(userid);
    Response.Cookies.Add(userCookie);
    var json
    = new { url = Url.Action("Index", "Main") };
    return Json(json, JsonRequestBehavior.AllowGet);
    //return RedirectToAction("Index", "Main");
    }
    }
    catch (Exception ex)
    {
    var json
    = ex.Message;
    return Json(json, JsonRequestBehavior.AllowGet);
    }
    }


      

  • 相关阅读:
    51keil编译器printf函数
    asp.net里登陆记住密码
    Asp.net GridView分页
    DataTable拆分分页
    ASP.NET MVC 窗体身份验证及角色权限治理示例
    asp.net获取IP地址
    Asp.net Ajax框架教程
    将页面的ViewState放在Session
    20个Jquery表单插件
    前端下载图片的N种方法
  • 原文地址:https://www.cnblogs.com/yannis/p/2139311.html
Copyright © 2020-2023  润新知