首页 > 代码库 > MVC之排球比赛计分程序 ——(八)具体代码(2)
MVC之排球比赛计分程序 ——(八)具体代码(2)
三、TeamController具体代码如下:
public class TeamController : Controller
{
private CountScoreDBContext db = new CountScoreDBContext();
//
// GET: /Team/
public ActionResult Index()
{
return View(db.Team.ToList());
}
//
// GET: /Team/Details/5
public ActionResult Details(int id = 0)
{
Team team = db.Team.Find(id);
if (team == null)
{
return HttpNotFound();
}
return View(team);
}
//
// GET: /Team/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Team/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Team team)
{
if (ModelState.IsValid)
{
db.Team.Add(team);
db.SaveChanges();
return RedirectToAction("Create","Score");
}
return View(team);
}
//
// GET: /Team/Edit/5
public ActionResult Edit(int id = 0)
{
Team team = db.Team.Find(id);
if (team == null)
{
return HttpNotFound();
}
return View(team);
}
//
// POST: /Team/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Team team)
{
if (ModelState.IsValid)
{
db.Entry(team).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(team);
}
//
// GET: /Team/Delete/5
public ActionResult Delete(int id = 0)
{
Team team = db.Team.Find(id);
if (team == null)
{
return HttpNotFound();
}
return View(team);
}
//
// POST: /Team/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Team team = db.Team.Find(id);
db.Team.Remove(team);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
视图:
1、Index.cshtml
@model IEnumerable<MvcVolleyball.Models.Team>
@{
ViewBag.Title = "Index";
}
<h2>开始比赛</h2>
<p>
@Html.ActionLink("添加新的比赛队伍", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.TName)
</th>
<th>
@Html.DisplayNameFor(model => model.TResult)
</th>
<th>
@Html.DisplayNameFor(model => model.TParentId)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TName)
</td>
<td>
@Html.DisplayFor(modelItem => item.TResult)
</td>
<td>
@Html.DisplayFor(modelItem => item.TParentId)
</td>
<td>
@Html.ActionLink("编辑", "Edit", new { id=item.TId }) |
@Html.ActionLink("详细信息", "Details", new { id=item.TId }) |
@Html.ActionLink("删除比赛", "Delete", new { id=item.TId })
</td>
</tr>
}
</table>
2、Edit.cshtml
@model MvcVolleyball.Models.Team
@{
ViewBag.Title = "Edit";
}
<h2>修改数据</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Team</legend>
@Html.HiddenFor(model => model.TId)
<div class="editor-label">
@Html.LabelFor(model => model.TName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TName)
@Html.ValidationMessageFor(model => model.TName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TResult)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TResult)
@Html.ValidationMessageFor(model => model.TResult)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TParentId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TParentId)
@Html.ValidationMessageFor(model => model.TParentId)
</div>
<p>
<input type="submit" value="http://www.mamicode.com/保存" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("返回首页", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
3、Details.cshtml
@model MvcVolleyball.Models.Team
@{
ViewBag.Title = "Details";
}
<h2>详细信息</h2>
<fieldset>
<legend>Team</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.TName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.TResult)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TResult)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.TParentId)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TParentId)
</div>
</fieldset>
<p>
@Html.ActionLink("编辑", "Edit", new { id=Model.TId }) |
@Html.ActionLink("详细信息", "Index","Score" )
@Html.ActionLink("返回首页", "Index")
</p>
4、Delete.cshtml
@model MvcVolleyball.Models.Team
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>确定删除?</h3>
<fieldset>
<legend>Team</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.TName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.TResult)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TResult)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.TParentId)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TParentId)
</div>
</fieldset>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<p>
<input type="submit" value="http://www.mamicode.com/删除" /> |
@Html.ActionLink("返回首页", "Index")
</p>
}
5、Create.cshtml
@model MvcVolleyball.Models.Team
@{
ViewBag.Title = "Create";
}
<h2>请输入比赛双方队伍名称</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Team</legend>
<div class="editor-label">
@Html.LabelFor(model => model.TName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TName)
@Html.ValidationMessageFor(model => model.TName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TResult)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TResult)
@Html.ValidationMessageFor(model => model.TResult)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TParentId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TParentId)
@Html.ValidationMessageFor(model => model.TParentId)
</div>
<p>
<input type="submit" value="http://www.mamicode.com/添加" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
四、SpecatorController具体代码如下:
public class SpectatorController : Controller
{
//
// GET: /Spectator/
private CountScoreDBContext db = new CountScoreDBContext();
public ActionResult SearchIndex(string searchString)
{
var team = from m in db.Team
select m;
if (!String.IsNullOrEmpty(searchString))
{
team = team.Where(s => s.TName.Contains(searchString));
}
return View(team);
}
public ActionResult SearchJu(int id=0)
{
Ju ju = db.Ju.Find(id);
if (ju == null)
{
return HttpNotFound();
}
return View(new List<Ju>() { ju });
//var juu = from m in db.Ju
// select m;
//if (juu == null)
//{
// juu = juu.Where(s => s.TJId == id);
//}
//return View(ju);
}
public ActionResult SearchScore()
{
return View(db.Score.ToList());
}
1、SearchIndex.cshtml
@model IEnumerable<MvcVolleyball.Models.Team>
@{
ViewBag.Title = "SearchIndex";
}
<h2>观众界面</h2>
<p>
@*@Html.ActionLink("Create New", "Create")*@
@using (Html.BeginForm()){
<p> Title: @Html.TextBox("SearchString") <br />
<input type="submit" value="http://www.mamicode.com/搜索" /></p>
}
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.TName)
</th>
<th>
@Html.DisplayNameFor(model => model.TResult)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TName)
</td>
<td>
@Html.DisplayFor(modelItem => item.TResult)
</td>
<td>
@*@Html.ActionLink("Edit", "Edit", new { id=item.TId }) |*@
@Html.ActionLink("详细","SearchJu", "Spectator",new { id=item.TId })
@* |@Html.ActionLink("Delete", "Delete", new { id=item.TId })*@
</td>
</tr>
}
</table>
2、SearchJu.cshtml
@model IEnumerable<MvcVolleyball.Models.Ju>
@{
ViewBag.Title = "SearchJu";
}
<h2>局分</h2>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.JUCi)
</th>
<th>
@Html.DisplayNameFor(model => model.JScore)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.JUCi)
</td>
<td>
@Html.DisplayFor(modelItem => item.JScore)
</td>
</tr>
}
@Html.ActionLink("详细", "SearchScore","Spectator")
</table>
<div>
@Html.ActionLink("返回首页", "SearchIndex")
</div>
3、SearchScore.cshtml
@model IEnumerable<MvcVolleyball.Models.Score>
@{
ViewBag.Title = "Index";
}
<h2>具体分数</h2>
<p>
@Html.ActionLink("返回首页", "SearchIndex")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.AScore)
</th>
<th>
@Html.DisplayNameFor(model => model.BScore)
</th>
<th>
@Html.DisplayNameFor(model => model.Note)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.AScore)
</td>
<td>
@Html.DisplayFor(modelItem => item.BScore)
</td>
<td>
@Html.DisplayFor(modelItem => item.Note)
</td>
</tr>
}
</table>
@Html.ActionLink("返回上一层", "SearchJu","Spectator")
测试了软件的功能,都能实现其功能。到这里,排球计分程序,算是完成了初步的功能与实现。也可以使用它的功能来记录分数了,在未来的日子里,还将要对此软件进行更深一步的研究,与设计,目前此软件的功能就先做到这里。到这里,我们的博客也快要结束了。在这个阶段的最后一篇博客里,我们将要讲述对此软件制作的总结与经验,也算是对此软件制作的一个小收尾。
MVC之排球比赛计分程序 ——(八)具体代码(2)