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();//生成验证码
Session["validateCode"] = code;//存储于Session中
byte[] buffer = validateCode.CreateValidateGraphic(code);//创建验证码的图片
return File(buffer, "image/jpeg");
}
}
}

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,),
new SqlParameter("@UserPwd",SqlDbType.NVarChar,)
//SqlDbType.NVarChar 数据库中的类型
};
pars[].Value = username;
pars[].Value = userpwd;
DataTable dt = DAL.SqlHelper.SelectSqlReturnDataTable(sql, CommandType.Text, pars);
T_UserInfo userinfo = null;
if (dt.Rows.Count > )
{
userinfo = new T_UserInfo();
LoadEntity(dt.Rows[], 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"]);
} }
}

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);
}
}
}

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"
} }

最新文章

  1. CDN技术详解
  2. 我收录整理的优秀OC技术类文章
  3. Hadoop 生态系统
  4. 计算几何 : 凸包学习笔记 --- Graham 扫描法
  5. RSA密钥的生成与配置
  6. CSS笔记(七)列表
  7. 如何使用VSTS做压力测试
  8. json 转 javaBean
  9. Toast的使用具体解释
  10. Robotium API -- 等待执行的方法sleep、waitFor
  11. SQL Profiler工具简介
  12. time date 基础操作
  13. js基本类型与引用类型,浅拷贝和深拷贝
  14. Luogu3613 睡觉困难综合征
  15. .net开发COM组件之组件签名&amp;注册
  16. 关于隐式创建vue实例实现简化弹出框组件显示步骤
  17. 量化投资与Python
  18. CTF显隐术:九连环
  19. NoHttp封装--02 自定义请求
  20. 【大数据】大数据处理-Lambda架构-Kappa架构

热门文章

  1. 【Nginx】 linux环境下安装nginx步骤
  2. leetcode题目4.寻找两个有序数组的中位数(困难)
  3. 邻居子系统 之 更新neigh_update
  4. Anaconda官网下载太慢/出错,以及Anaconda下载包又慢又出错的总体方法,应该如何快速下载,使用上海科技大学的开源镜像站即可
  5. R语言:读入txt文件中文文本出现乱码解决方案
  6. php 获取域名
  7. Selenium2Library测试web
  8. Jmeter测试结果分析(上)
  9. Python的Multiprocessing多进程实例
  10. 虚拟机安装: vmware + ubuntu16.04