C# MVC结合AJAX实现登录窗口(新手看)


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

    }
}


    
    登录窗口


    


你可能感兴趣的:(MVC学习之旅)