04.Model

4.1 添加Model

直接添加Class

public class MovieContext:DbContext
{
  public DbSet Movies{get;set;}
}
//连接数据库
//Web.config
//DefaultConnect
//或者自己添加

4.2 添加Controller

Code first
Add_MVC5 Controller with Views,EF

4.3 页面到Active

using System.ConponetModel.DataAnnotations;
[Display(Name="Relesa Data")]//显示名字
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString="{0:yyyy-MM-dd}",ApplyFormatInEditMode=true)]

//页面跳转
//这个是路由,再App_Data文件夹下
@Html.ActiveLink("Delete","Delete",new{id=item.ID})

//Get
public ActionResult Edit(int? id)
{
//空返回
retrun new HttpStatusCodeResult(HttpStatusCode.BadRequest)
}
//没有找到
return HttpNotFound();
//找到
return View(moves)
//前端@model MVC.Models.Movies,以后可以用@model访问刚才找到的东西
//post
[HttpPost]
[ValidateAntiForgeryToken]//防止跨页请求
public ActionReuslt Edit([Bind(Inclue="ID,Title,ReleaseDate,Genre,Price")] Move move)//绑定属性
{
  if(ModelState.IsValid)
  {
    db.Entry(move).state=EntityState.Modified;
    db.SaveChanges();
    return RedirectRToAction("Index");
  }
 return View(movie)
}

//页面防止跨页请求
@Html.AntiForgeryToken()

4.4 解析Detail和Delete

//Detail只有一个
//可以空的ID
//和Edit的类似

//Delete的Get类似
//Post
[HttpPost,ActionName("Delete")]//指定方法对应的功能连接
[ValidateAntiForgeryToken]//防止跨页请求
public ActionResult DeleteConfirmed(int id)
{
 //先找
 //Remove,SaveChanges
 return RedirectToAction("Index")
}

4.5 ViewBag,Model

//ViewData["a b c"]是object,使用的时候需要转换
//ViewBag是动态类型
//TempData只能读取一次,可以在各个的
//总结:TempData也是一個String Key/Object Value字典陣列。
// 和ViewData與ViewBag不同的是其所儲存的資料物件的生命週期。 如果頁面發生了跳轉(Redirection),ViewBag和ViewData中的值將不復存在, 但是TempData中的值依然還在。 
//換句話講, ViewBag和ViewData儲存的值的生命週期只有在從Controller到View中, 而TempData中的資料不僅在從Controller到View中有效,在不同的Action之間或者從一個頁面跳轉到另一頁面(Controller to Controller)後依然有效。
//Model 
//要在前台先引用,前台直接return(personList);
@model 类型
@for(var temp in Model)
{
}

4.6 using

@using Person
//或在web.config下添加引用

你可能感兴趣的:(04.Model)