using LoginDemo.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace LoginDemo.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private LoginDemoContext db = new LoginDemoContext();
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Login(string Account, string Pwd)
{
ContentResult Content = new ContentResult();
Content.ContentType = "text/html";
if (string.IsNullOrEmpty(Account))
{
return RedirectToAction("index");
}
if (string.IsNullOrEmpty(Pwd))
{
return RedirectToAction("index");
}
LoginUser data = db.LoginUsers.Where(p => p.Account == Account.Trim()
&& p.Pwd == Pwd.Trim()).FirstOrDefault();
if (data == null)
{
return RedirectToAction("index");
}
HttpContext.Session.SetString("account", data.Account);
return RedirectToAction("list");
}
public IActionResult list()
{
List<LoginUser> data = db.LoginUsers.ToList();
if (HttpContext.Session.GetString("account") != null)
{
ViewBag.Account = HttpContext.Session.GetString("account");
}
else
{
return RedirectToAction("index");
}
return View(data);
}
public IActionResult add()
{;
return View();
}
public IActionResult edit(int id)
{
return View(db.LoginUsers.Find(id));
}
public IActionResult delete(int id)
{
LoginUser data = db.LoginUsers.Find(id);
db.LoginUsers.Remove(data);
db.SaveChanges();
return RedirectToAction("list");
}
public IActionResult save(LoginUser loginUser)
{
if (loginUser.Id > 0)
{
//修改
LoginUser data= db.LoginUsers.Find(loginUser.Id);
if (data != null)
{
data.Name = loginUser.Name;
data.Sex = loginUser.Sex;
data.Account = loginUser.Account;
data.Address = loginUser.Address;
data.Pwd = loginUser.Pwd;
}
}
else
{
//新增
db.LoginUsers.Add(loginUser);
}
db.SaveChanges();
return RedirectToAction("list");
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}