实现网上书店的例子2

选择Internet模板

1.创建登录模型类Login

 public class Login
    {
        public int id { get; set; }
        public string name { get; set; }
        public string password { get; set; }

        public virtual ICollection<Order> Orders { get; set; }//外键一对多
    }

2.创建模型类Order

public class Order
    {
        public int id { get; set; }
        public DateTime Time { get; set; }
        public decimal Price { get; set; }

        public virtual Login Logins { get; set; }//外键一对多
    }

3.创建数据库上下文类

public class Dbcontext:DbContext
    {
        public DbSet<Login> Logins { get; set; }
        public DbSet<Order> Orders { get; set; }
    }

4.数据迁移:视图->其它窗口->程序包管理器

enable-

add-

update-

更改webconfig中connectionstring中的名字与数据库上下文类相同

5.创建控制器Home

public class HomeController : Controller
    {
        private Dbcontext db = new Dbcontext();

               public ActionResult Index()
        {
            return View(db.Orders.ToList());
        }

             public ActionResult Details(int id = 0)
        {
            Order order = db.Orders.Find(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            return View(order);
        }

   

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

6.创建控制器Account

 public class AccountController : Controller
    {
        Dbcontext db = new Dbcontext();

        [HttpGet]
        public ActionResult Login()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Login(Login User)
        {
            var user = db.Logins.Where(a => a.name == User.name && a.password == User.password).FirstOrDefault();
            if (user != null)
                return RedirectToAction("Index", "Home");
            return View();
        }

    }

[HttpPost]
public ActionResult Login(Login admin)
{
var user = db.Admins.Where(a => a.UserName == admin.UserName
&& a.Password == admin.Password).FirstOrDefault();
if (user != null)
{
//创建一个新的票据,将用户的名字记入ticket的userdata 
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(20),
false, user.AdminRole.ToString());
//将票据加密 
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
//将加密后的票据保存为cookie 
System.Web.HttpCookie authCookie = new System.Web.HttpCookie
(FormsAuthentication.FormsCookieName, encryptedTicket);
//使用加入了userdata的新cookie 
System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
return RedirectToAction("Index", "Admin");
}
return View();
}

 

7.创建视图Index

@model IEnumerable<_3.Models.Order>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
你的订单如下:
@foreach (var item in Model)
{
<p>bianhao: @(item.id)</p>
    @Html.ActionLink("xxxx", "details", new {id=@item.id })
    <hr/>
}

8.创建视图Detail

强类型视图

选择Detail模板

9.创建视图Login

强类型视图,选择Login模型,选择create模版

@if (Request.IsAuthenticated)
{
<text>
你好,@Html.ActionLink(User.Identity.Name, "Manage", "Admin", routeValues: null, htmlAttributes: new { @class = "username", title = "管理" })!
@using (Html.BeginForm("LoginOut", "Account", FormMethod.Post, new { id = "LoginOutForm" }))
{
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('LoginOutForm').submit()">注销</a>
}
</text>
}
else
{ <text>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<p>
@Html.ActionLink("登录", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })
@Html.ActionLink("注册", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })
</p>
}
</text>
}

 

posted @ 2016-01-05 10:08  butterfly小d  阅读(148)  评论(0编辑  收藏  举报