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

oa_mvc_easyui_登录完成(2)的更多相关文章

  1. oa_mvc_easyui_项目搭建及登录页面验证码(1)

    1.空项目的搭建,三层的搭建(各层之中的引用) webapp:bll,model,common bll:dal,model dal:model 2.SQL表 ItcastDb:T_UserInfo,T ...

  2. 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态

    最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...

  3. TODO:Laravel 内置简单登录

    TODO:Laravel 内置简单登录 1. 激活Laravel的Auth系统Laravel 利用 PHP 的新特性 trait 内置了非常完善好用的简单用户登录注册功能,适合一些不需要复杂用户权限管 ...

  4. web全栈开发之网站开发二(弹出式登录注册框前端实现-类腾讯)

    这次给大家分享的是目前很多网站中流行的弹出式登录框,如下面的腾讯网登录界面,采用弹出式登录的好处是大大提升了网站的用户体验和交互性,用户不用重新跳转到指定的页面就能登录,非常方便 先来个演示地址 要实 ...

  5. 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】

    说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...

  6. PHP验证用户登录例子-学习笔记

    1.基本流程: 2.UML类图: 3.PHP代码: 3.1 index.php <?php /** * Created by PhpStorm. * User: andy * Date: 16- ...

  7. AJAX实现登录界面

    使用php跳转界面和AJAX都可实现登录界面的跳转的登录失败对的提醒.但是,php跳转的方式 需要额外加载其他界面,用户体验差.AJAX可实现当前页面只刷新需要的数据,不对当前网页进行 重新加载或者是 ...

  8. 微信网页开发之获取用户unionID的两种方法--基于微信的多点登录用户识别

    假设网站A有以下功能需求:1,pc端微信扫码登录:2,微信浏览器中的静默登录功能需求,这两种需求就需要用到用户的unionID,这样才能在多个登录点(终端)识别用户.那么这两种需求下用户的unionI ...

  9. 基于SignalR的消息推送与二维码描登录实现

    1 概要说明 使用微信扫描登录相信大家都不会陌生吧,二维码与手机结合产生了不同应用场景,基于二维码的应用更是比较广泛.为了满足ios.android客户端与web短信平台的结合,特开发了基于Singl ...

随机推荐

  1. pytype

    与mypy相比不仅可以显示错误行数,还可以看到哪个函数错误. mypy的图 pytype的图

  2. CodeForce 137B

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description " ...

  3. 基于XML的AOP配置(2)-环绕通知

    配置方式: <aop:config> <aop:pointcut expression="execution(* com.itheima.service.impl.*.*( ...

  4. DP&图论 DAY 1 下午

    DP&图论  DAY 1  下午  区间和序列上的DP 序列上的DP >序列上的dp状态设计最基本的形式 F[i]表示以 i 结尾的最优值或方案数.◦ F[i][k]表示以 i 结尾附加 ...

  5. LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】

    Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...

  6. ControlTemplate in WPF —— DataGrid

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  7. js高程之作用域

    我们知道js执行环境有全局环境(window)和局部环境(一般指函数环境)之分. ; function calc(){ ; } 上述代码,虽然有两个num变量,但是他们所在的执行环境却是不同的,第一个 ...

  8. JavaScript基础之数组常用方法

    目录 JS 数组常用API 常用属性 常用方法 常见方法语法解释 from方法 isArray concat every fill filter find forEach indexOf join k ...

  9. (转)使用 HTML5 WebSocket 构建实时 Web 应用

    HTML5 WebSocket 简介和实战演练 本文主要介绍了 HTML5 WebSocket 的原理以及它给实时 Web 开发带来的革命性的创新,并通过一个 WebSocket 服务器和客户端的案例 ...

  10. Java中流的操作练习

    文件中的学生信息 学生信息存储在TXT文件中,我们需要对信息进行基本的,增.删.改.查,全部显示操作. 1.学生类/Student package com.yujiao.student; public ...