1.首先实现的是Vollerballs的控制器:
利用entity framework自动生成控制器,用实体类VolleyballDB为模型和VolleyballContext为上下文自动创建Controller的方法和增删改查的视图。
2.之后再自定义一个Home控制器,实现把排球计分信息传入数据库,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
private VolleyballContext db = new VolleyballContext();
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Play(VolleyballDB db)
{
ViewData.Add("nameGame", db.gameName);
ViewData.Add("nameA", db.teamA);
ViewData.Add("nameB", db.teamB);
return View();
}
[HttpPost]
public ActionResult PlayRe(VolleyballDB volleyballdb)
{
if (ModelState.IsValid)
{
db.Volleyballs.Add(volleyballdb);
db.SaveChanges();
return RedirectToAction("../Volleyballs/Index");
}
return View("Play", volleyballdb);
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
3.把自动生成的Create视图和对应Action删除,并且修改相应指向。