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="保存" />
        </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="删除" /> |
        @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="添加" />
        </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="搜索" /></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")

测试了软件的功能,都能实现其功能。到这里,排球计分程序,算是完成了初步的功能与实现。也可以使用它的功能来记录分数了,在未来的日子里,还将要对此软件进行更深一步的研究,与设计,目前此软件的功能就先做到这里。到这里,我们的博客也快要结束了。在这个阶段的最后一篇博客里,我们将要讲述对此软件制作的总结与经验,也算是对此软件制作的一个小收尾。

时间: 2024-07-29 02:36:10

MVC之排球比赛计分程序 ——(八)具体代码(2)的相关文章

MVC之排球比赛计分程序 ——(一)需求分析与数据库设计

在实际的项目中,需求分析和数据库的设计是很重要的一个环节,这个环节会直接影响项目的开发过程和质量.实际中,这个环节不但需要系统分析师.软件工程师等计算机方面的专家,还需要相关领域的领域专家参与才能完成. 需求分析: 这个项目是一个排球比赛计分程序,其业务极为简单,现将其描述如下. 1.任何观众都可以进行比赛的分数查询,查询完成后,页面上显示查询的相应的比赛内容. 2.任何观众都不可以对分数进行增删改查. 3.记分员可以对比赛进行实时记录,并将分数记录在数据库,方便观众查询,以及对分数进行通过操作

MVC之排球比赛计分程序 ——(七)具体实现

1,新建一个项目,命名为:Volleyball,选择基本模板.如图: 点击确定.创建项目. 2,右键单击model文件夹,添加模型类:模型类分别是:GzScore.cs和Players.cs 具体代码如下: public class Team    {        [Key]        public int TId { get; set; }        [Display(Name = "队伍名称")]        public string TName { get; set

MVC之排球比赛计分程序 ——(五)控制器的设计与实现

控制器 控制器接受用户的输入并调用模型和视图去完成用户的需求.所以当单击Web页面中的超链接和发送HTML表单时, 控制器本身不输出任何东西和做任何处理.它只是接收请求并决定调用哪个模型构件去处理请求, 然后用确定用哪个视图来显示模型处理返回的数据. Controller控制器接受用户请求,然后返回视图.控制器控制视图的产生.我们根据此软件的需求,设计所 需要的Controller.我们添加控制器就需要放到controller文件夹里. 我们为实现此软件的需求,目前我们需要五个Controlle

MVC之排球比赛计分程序 ——(九)总结

系列博客目的是制作一款排球计分程序.这系列博客将讲述此软件的各个功能的设计与实现.到这篇博客,此系列博客就算是结束了.在最后的这篇博客里 我们来做一些总结. 一,制作此程序,我们使用的是MVC框架.MVC是一种程序开发设计模式,它实现了显示模块与功能模块的分离.提高了程序的可维护性.可移植性.可扩展性与可重用性,降低了程序的开发难度.它主要分模型.视图.控制器三层. 使用MVC有诸多好处: 1:耦合性低 视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样,一个应用的

MVC之排球比赛计分程序 ——(八)具体代码(1)

一.JuController代码如下: public class JuController : Controller    {        private CountScoreDBContext db = new CountScoreDBContext(); //        // GET: /Ju/ public ActionResult Index()        {            return View(db.Ju.ToList());        } //       

MVC之排球比赛计分程序 ——(二)架构概要设计

本程序主要基于MVC4框架,使应用程序的输入,处理和输出强制性分开,使得软件可维护性,可扩展性,灵活性以及封装性得到提高, MVC应用程序分为三个核心部件:Model,View, Controller. 一, 架构基本原则: MVC是一个设计模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务. 视图  视图是用户看到并与之交互的界面.对老式的Web应用程序来说,视图就是由HTML元素组成的界面,在新式的Web应用程序

MVC之排球比赛计分程序 ——(四)视图的设计与实现

(view)视图  视图是用户看到并与之交互的界面.对老式的Web应用程序来说,视图就是由HTML元素组成的界面,在新式的Web应用程序中,HTML依旧在视图中扮演着重要的角色,但一些新的技术已层出不穷,它们包括Macromedia Flash和象XHTML,XML/XSL,WML等一些标识语言和Web services. 此软件的视图为用户提供可视化的界面及操作.使用户能清楚明白的使用软件的功能.view 通过controler的调用呈现给用户: 设计视图之前来看一下视图要放在哪个功能之内:

MVC之排球比赛计分程序 ——(六)使用框架,创建控制器,生成数据库

在上篇博客我们写到,此软件的数据库连接我们使用的是EF框架,code first模式下,通过模型类,在创建controller的时候直接生成数据库,完成数据库的连接,与操作. 在使用EF框架之前,我们需要写好模型类.然后在创建controller. 此软件目前需要两个模型类,在之前的博客中,我们已经设计,和完成了模型类,这时候我们只需把代码拿过来就可以使用了.这里包括三个类文件:Team.cs, Ju.cs,Score.cs,分别是队伍类.分数类和局次类.具体代码如下: public class

MVC之排球比赛计分程序 ——(三)model类的设计与实现

实体类是现实实体在计算机中的表示.它贯穿于整个架构,负担着在各层次及模块间传递数据的职责.一般来说,实体类可以分为"贫血实体类"和"充血实体类",前者仅仅保存实体的属性,而后者还包含一些实体间的关系与逻辑. 大多情况下,实体类和数据库中的表(这里指实体表,不包括表示多对多对应的关系表)是一一对应的,但这并不是一个限制,在复杂的数据库设计中,有可能出现一个实体类对应多个表,或者交叉对应的情况.在本文的Demo中,实体类和表是一一对应的,并且实体类中的属性和表中的字段也