oa_mvc_easyui_登录完成(2)

1.使用MVC特有的ajax方式:异步表单提交方式

  js文件引用:<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

  代码:

 @using(Ajax.BeginForm("UserLogin",new{},new AjaxOptions(){OnSuccess="afterLogin"},new{id="loginForm"})){
......
//相关的表单
}

**

使用Ajax.BeginForm方法会生成一个form表单,最后以Ajax的方式提交表单数据;需要用using把该方法括起来,使系统知道form表单从何处开始,何处结束。

 new AjaxOptions(){...} -- ajax的一些参数

 new {id="loginForm"} -- htmlAttributes,html属性,生成form表单时,会把键值对添加到form表单的属性中

 new{} -- routeValues...

 "CheckLogin" -- 指定请求地址的Action名称

参考:http://www.cnblogs.com/zzgblog/p/5454019.html?_t=t

        http://www.cnblogs.com/haof3344/p/4659040.html

 

2. 控制器LoginController中编写登录的方法:UserLogin(),用于异步ajax刷新

 LoginController:UserLogin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Itcast.CMS.Model;
using Itcast.CMS.BLL;

namespace Itcast.CMS.WebApp.Controllers
{
    public class LoginController : Controller
    {
        //约定大于配置
        // GET: /Login/

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

        public ActionResult UserLogin()
        {
            //判断Session中的验证码是否为空
            string validateconde = Session["validateCode"].ToString() == null ? string.Empty : Session["validateCode"].ToString();
            if(string.IsNullOrEmpty(validateconde))
            {
                return Content("no:验证码错误!");
            }
            Session["validateCode"] = null;
            //判断验证码是否正确
            string vcode = Request["vCode"].ToString();
            if(!vcode.Equals(validateconde,StringComparison.InvariantCultureIgnoreCase))
            {
                //StringComparison.InvariantCultureIgnoreCase 使用区域敏感排序规则、固定区域来比较字符串,同时忽略被比较字符串的大小写
                return Content("no:验证码错误!");
            }
            //判断用户名和密码
            string username = Request["LoginCode"];
            string userpwd = Request["LoginPwd"];
            UserInfoService UserInfoService = new UserInfoService();
            T_UserInfo UserInfo = UserInfoService.GetUserInfoMode(username,userpwd);
            if (UserInfo != null)
            {
                Session["UserInfo"] = UserInfo;//UserInfo对象赋值给Session,
                //调用((T_UserInfo)Session["UserInfo"]).UserName;
                return Content("ok:登录成功!!");
            }
            else
            {
                return Content("no:用户名密码错误");
            }
        }

        /// <summary>
        /// 生产验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult ValidateCode()
        {
            Common.ValidateCode validateCode = new Common.ValidateCode();
            string code = validateCode.CreateValidateCode(4);//生成验证码
            Session["validateCode"] = code;//存储于Session中
            byte[] buffer = validateCode.CreateValidateGraphic(code);//创建验证码的图片
            return File(buffer, "image/jpeg");
        }
    }
}
View Code

 

3.UserInfoDal层中编写具体方法GetUserInfoMode,LoadEntity用户赋值给T_UserInfo类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Itcast.CMS.Model;
using System.Data.SqlClient;
using System.Data;

namespace Itcast.CMS.DAL
{
    public class UserInfoDal
    {

        public T_UserInfo GetUserInfoMode(string username, string userpwd)
        {
            string sql = " select * from T_UserInfo where UserName=@UserName and UserPwd=@UserPwd ";
            SqlParameter[] pars = {
                                       new SqlParameter("@UserName",SqlDbType.NVarChar,50),
                                       new SqlParameter("@UserPwd",SqlDbType.NVarChar,50)
                                       //SqlDbType.NVarChar 数据库中的类型
                                   };
            pars[0].Value = username;
            pars[1].Value = userpwd;
            DataTable dt = DAL.SqlHelper.SelectSqlReturnDataTable(sql, CommandType.Text, pars);
            T_UserInfo userinfo = null;
            if (dt.Rows.Count > 0)
            {
                userinfo = new T_UserInfo();
                LoadEntity(dt.Rows[0], userinfo);
            }
            return userinfo;
        }

        public void LoadEntity(DataRow row, T_UserInfo userInfo)
        {
            userInfo.Id = Convert.ToInt32(row["id"].ToString());
            //DBNull.Value 判断是否为null值
            userInfo.UserName = row["UserName"] != DBNull.Value ? row["UserName"].ToString() : string.Empty;
            userInfo.UserPwd = row["UserPwd"] != DBNull.Value ? row["UserPwd"].ToString() : string.Empty;
            userInfo.UserMail = row["UserMail"] != DBNull.Value ? row["UserMail"].ToString() : string.Empty;
            userInfo.RegTime = Convert.ToDateTime(row["RegTime"]);
        }

    }
}
View Code

 

4.业务层:UserInfoService

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Itcast.CMS.Model;

namespace Itcast.CMS.BLL
{
    public class UserInfoService
    {
        DAL.UserInfoDal UserInfoDal = new DAL.UserInfoDal();
        public T_UserInfo GetUserInfoMode(string username, string userpwd)
        {
            return UserInfoDal.GetUserInfoMode(username, userpwd);
        }
    }
}
View Code

 

5.afterLogin回调函数,对后台返回的数据做处理

 function afterLogin(data) {

            var serverData = data.split(':');
            if (serverData[0] == "ok") {
                window.location.href = "/Home/Index"
            } else if (serverData[0] == "no") {
                $("#errorMsg").text(serverData[1]);
                changeCheckCode();
            } else {
                window.location.href = "/Error.html"
            }

        }
View Code

 

posted @ 2017-05-24 11:24  youguess  阅读(432)  评论(0编辑  收藏  举报