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

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

一、JuController代码如下:

  public class JuController : Controller
    {
        private CountScoreDBContext db = new CountScoreDBContext();

        //
        // GET: /Ju/

        public ActionResult Index()
        {
            return View(db.Ju.ToList());
        }

        //
        // GET: /Ju/Details/5

        public ActionResult Details(int id = 0)
        {
            Ju ju = db.Ju.Find(id);
            if (ju == null)
            {
                return HttpNotFound();
            }
            return View(ju);
        }

        //
        // GET: /Ju/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Ju/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Ju ju)
        {
            if (ModelState.IsValid)
            {
                db.Ju.Add(ju);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(ju);
        }

        //
        // GET: /Ju/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Ju ju = db.Ju.Find(id);
            if (ju == null)
            {
                return HttpNotFound();
            }
            return View(ju);
        }

        //
        // POST: /Ju/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Ju ju)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ju).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ju);
        }

        //
        // GET: /Ju/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Ju ju = db.Ju.Find(id);
            if (ju == null)
            {
                return HttpNotFound();
            }
            return View(ju);
        }

        //
        // POST: /Ju/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Ju ju = db.Ju.Find(id);
            db.Ju.Remove(ju);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

所对应的增删改查视图:

1、Index.cshtml

@model IEnumerable<MvcVolleyball.Models.Ju>

@{
    ViewBag.Title = "Index";
}

<h2>局分首页</h2>

<p>
    @Html.ActionLink("添加局分数据", "Create")
</p>
<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>
      
        <td>
            @Html.ActionLink("编辑", "Edit", new { id=item.JId }) |
            @Html.ActionLink("详细信息", "Details", new { id=item.JId }) |
            @Html.ActionLink("删除", "Delete", new { id=item.JId })
        </td>
    </tr>
}

</table>
  @Html.ActionLink("返回首页", "Index","Team")

2、Edit.cshtml

@model MvcVolleyball.Models.Ju

@{
    ViewBag.Title = "Edit";
}

<h2>编辑</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Ju</legend>

        @Html.HiddenFor(model => model.JId)

        <div class="editor-label">
            @Html.LabelFor(model => model.JUCi)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.JUCi)
            @Html.ValidationMessageFor(model => model.JUCi)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.JScore)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.JScore)
            @Html.ValidationMessageFor(model => model.JScore)
        </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.Ju

@{
    ViewBag.Title = "Details";
}

<h2>详细信息</h2>

<fieldset>
    <legend>Ju</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.JUCi)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.JUCi)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.JScore)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.JScore)
    </div>

 
</fieldset>
<p>
    @Html.ActionLink("编辑", "Edit", new { id=Model.JId }) |
    @Html.ActionLink("返回首页", "Index")
</p>

4、Delete.cshtml

@model MvcVolleyball.Models.Ju

@{
    ViewBag.Title = "Delete";
}

<h2>删除</h2>

<h3>确认删除?</h3>
<fieldset>
    <legend>Ju</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.JUCi)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.JUCi)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.JScore)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.JScore)
    </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.Ju

@{
    ViewBag.Title = "Create";
}

<h2>添加数据</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Ju</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.JUCi)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.JUCi)
            @Html.ValidationMessageFor(model => model.JUCi)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.JScore)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.JScore)
            @Html.ValidationMessageFor(model => model.JScore)
        </div>


        <p>
            <input type="submit" value="http://www.mamicode.com/添加" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("返回首页", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

二、ScoreController代码如下:

 public class ScoreController : Controller
    {
        private CountScoreDBContext db = new CountScoreDBContext();

        //
        // GET: /Score/

        public ActionResult Index()
        {
            return View(db.Score.ToList());
        }

        //
        // GET: /Score/Details/5

        public ActionResult Details(int id = 0)
        {
            Score score = db.Score.Find(id);
            if (score == null)
            {
                return HttpNotFound();
            }
            return View(score);
        }

        //
        // GET: /Score/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Score/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Score score)
        {
            if (ModelState.IsValid)
            {
                db.Score.Add(score);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(score);
        }

        //
        // GET: /Score/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Score score = db.Score.Find(id);
            if (score == null)
            {
                return HttpNotFound();
            }
            return View(score);
        }

        //
        // POST: /Score/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Score score)
        {
            if (ModelState.IsValid)
            {
                db.Entry(score).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(score);
        }

        //
        // GET: /Score/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Score score = db.Score.Find(id);
            if (score == null)
            {
                return HttpNotFound();
            }
            return View(score);
        }

        //
        // POST: /Score/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Score score = db.Score.Find(id);
            db.Score.Remove(score);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

1、Create.cshtml

@model MvcVolleyball.Models.Score

@{
    ViewBag.Title = "Create";
}

<h2>添加数据</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Score</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.AScore)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AScore)
            @Html.ValidationMessageFor(model => model.AScore)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.BScore)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BScore)
            @Html.ValidationMessageFor(model => model.BScore)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Note)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Note)
            @Html.ValidationMessageFor(model => model.Note)
        </div>

    

        <p>
            <input type="submit" value="http://www.mamicode.com/添加" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("返回分数首页", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

2、Delete.cshtml

@model MvcVolleyball.Models.Score

@{
    ViewBag.Title = "Delete";
}

<h2>删除</h2>

<h3>确认删除?</h3>
<fieldset>
    <legend>Score</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.AScore)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.AScore)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.BScore)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.BScore)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Note)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Note)
    </div>

 
</fieldset>
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    <p>
        <input type="submit" value="http://www.mamicode.com/删除" /> |
        @Html.ActionLink("返回分数首页", "Index")
    </p>
}

3、Details.cshtml

@model MvcVolleyball.Models.Score

@{
    ViewBag.Title = "Details";
}

<h2>详细信息</h2>

<fieldset>
    <legend>Score</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.AScore)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.AScore)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.BScore)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.BScore)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Note)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Note)
    </div>

 
</fieldset>
<p>
    @Html.ActionLink("编辑", "Edit", new { id=Model.SId }) |
    @Html.ActionLink("返回分数首页", "Index")
</p>

4、Edit.cshtml

@model MvcVolleyball.Models.Score

@{
    ViewBag.Title = "Edit";
}

<h2>编辑</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Score</legend>

        @Html.HiddenFor(model => model.SId)

        <div class="editor-label">
            @Html.LabelFor(model => model.AScore)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AScore)
            @Html.ValidationMessageFor(model => model.AScore)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.BScore)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BScore)
            @Html.ValidationMessageFor(model => model.BScore)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Note)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Note)
            @Html.ValidationMessageFor(model => model.Note)
        </div>

      
        <p>
            <input type="submit" value="http://www.mamicode.com/保存" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("返回分数首页", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

5、Index.cshtml

@model IEnumerable<MvcVolleyball.Models.Score>

@{
    ViewBag.Title = "Index";
}

<h2>分数首页</h2>

<p>
    @Html.ActionLink("添加数据", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.AScore)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BScore)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Note)
        </th>
    
        <th></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>
      
        <td>
            @Html.ActionLink("编辑", "Edit", new { id=item.SId }) |
            @Html.ActionLink("详细信息", "Details", new { id=item.SId }) |
            @Html.ActionLink("删除", "Delete", new { id=item.SId })
        </td>
    </tr>
}

</table>
  @Html.ActionLink("返回局首页", "Index","Ju")

 

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