C# MVC结合AJAX实现登录窗口(新手看)
2018-03-07 21:10
459 查看
using System.Web.Mvc; using System.Windows.Forms; using MvcMySchool.Models; using static MvcMySchool.Models.Common; using System.Web.Routing; namespace MvcMySchool.Controllers { public class UsersController : Controller { // GET: Users public ActionResult Index() { return View(); } public ActionResult Login() { VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null,"Default",new RouteValueDictionary(new { })); return View(); } [HttpPost]//响应来自前台的请求 public ActionResult Login(string userName,string userPassWord)//参数个数、名字,要求和前台传来的一样 { string messge = "登陆成功!"; int code = 1; User user = null; LoginStatus status=UserManager.Login(userName, userPassWord, out user); if (status == LoginStatus.UserNameNoExisit)//LoginStatus枚举直 { messge = "用户名不存在!"; code = 0; } else if (status == LoginStatus.PassworeError) { messge = "密码不正确!"; code = 0; } JsonResult ajaxres = new JsonResult(); ajaxres.JsonRequestBehavior = JsonRequestBehavior.AllowGet; ajaxres.Data = new { messge = messge, code = code }; return ajaxres; } } }
<html> <head> <meta name="viewport" content="width=device-width" /> <title>登录窗口</title> </head> <body style="background-color:aquamarine"> <form> <input id="UserName" class="text-input" type="text" /> <input id="UserPassWord" class="text-center" /> <input id="button" class="submit" type="button" /> </form> <script src="~/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $("#button").click(function () { var userName = $("#UserName").val(); var userPassWord = $("#UserPassWord").val(); if(userName=="" || userPassWord=="") { alert("用户名或密码不能为空!"); return; } $.ajax({ type: 'POST', data: { userName: userName, userPassWord: userPassWord }, dataType: 'json', url: '@Url.Action("Login")', success: function (data) { if (data.code != 1) { alert(data.messge); } else { window.location.href='@Url.Action("Index","Home")' } } }) }) </script> </body> </html>
相关文章推荐
- 简单示例AJAX结合PHP代码实现登录效果代码
- xmlHTTP xmlDOC 与 C#中DataSet的结合 实现AJAX简单示例
- div滤镜结合ajax,实现登录
- C# Winform中实现主窗口打开登录窗口关闭的方法
- xmlHTTP xmlDOC 与 C#中DataSet的结合 实现AJAX简单示例
- MVC3学习:利用mvc3+ajax实现登录
- 自己的项目结合MVC实现ajax跨域调用
- Ajax与PHP结合实现登录验证
- Ajax中,ModalPopup与UpdatePanel结合,实现局部刷新的登录效果 (调用cs服务)
- C#登录窗口的实现(不用隐藏)
- 利用ajax实现前台javascript同c#后台MVC下的aspx.cs文件传递数据的两种方法2
- Ajax中,ModalPopup与UpdatePanel结合,实现局部刷新的登录效果 (调用cs服务)
- 简单示例:AJAX结合PHP代码实现登录
- C#结合js 上传文件和删除文件(技术点有:asp.net mvc ,nhibernate,ajax等)
- C#实现登录窗口(不用隐藏)
- 【MVC】会员注册/登录,普通验证,会员名是否注册Ajax验证以及会员邮件验证实现原理
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- div滤镜结合ajax,实现登录
- 力所能及之springmvc+ajax+jquery+json实现登录异步校验用户名是否存在
- C# Winform中实现主窗口打开登录窗口关闭的方法