标签:
创建Model
public class Order
{
public int id { get; set; }
public DateTime Time { get; set; }
public decimal Price { get; set; }
public virtual Login Logins { get; set; }
}
创建用户登录Model
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; }
}
创建上下文类
public class Dbcontext:DbContext
{
public DbSet<Login> Logins { get; set; }
public DbSet<Order> Orders { get; set; }
}
同志!!!!!!!记得修改web.config里面的数据库链接
创建Home控制器,选择读写,删除不需要的部分,模板选择Order,上下文类选择
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);
}
创建一个Account控制器
public class AccountController : Controller
{
//
// GET: /Account/
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();
}
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
创建Login视图,选择Create
@model _3.Models.Login
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Login</legend>
<div class="editor-label">
@Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.password)
@Html.ValidationMessageFor(model => model.password)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
创建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/>
}
标签:
原文地址:http://www.cnblogs.com/shenbinlei/p/5100370.html