您的位置:首页 > 其它

排球计分程序功能完善

2017-09-10 21:39 337 查看
利用暑假空闲时间来完善这个程序,增加了一个查询比赛的功能,并把以前的代码改进。

具体如下:

视图









首页代码

public class VolleyballController : Controller
{
private VolleballDBContext db = new VolleballDBContext();

//
// GET: /Volleyball/

public ActionResult Index()
{
return View(db.Volleyballs.ToList());
}
[HttpPost]
public ActionResult Index(string teamb, string searchString)
{
var VisitingteamLst = new List<string>();
var VisitingteamQry = from d in db.Volleyballs orderby d.Visitingteam select d.Visitingteam;
VisitingteamLst.AddRange(VisitingteamQry.Distinct());
ViewBag.teamb = new SelectList(VisitingteamLst);

var volleyballs = from v in db.Volleyballs select v;
if (!String.IsNullOrEmpty(searchString))
{
volleyballs = volleyballs.Where(s => s.Hometeam.Contains(searchString));
}
if (string.IsNullOrEmpty(teamb))
return View(volleyballs);
else
{
return View(volleyballs.Where(x => x.Visitingteam == teamb));
}

}

//
// GET: /Volleyball/Details/5

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

//
// GET: /Volleyball/Create

public ActionResult Create()
{
return RedirectToAction("Index","Score");
}

//
// POST: /Volleyball/Create

[HttpPost]
public ActionResult Create(Volleyball volleyball)
{
if (ModelState.IsValid)
{
db.Volleyballs.Add(volleyball);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(volleyball);
}

//
// GET: /Volleyball/Edit/5

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

//
// POST: /Volleyball/Edit/5

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

//
// GET: /Volleyball/Delete/5

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

//
// POST: /Volleyball/Delete/5

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

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


首页视图

@model IEnumerable<MvcApplication1.Models.Volleyball>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
@Html.ActionLink("创建比赛", "Create")

@using (Html.BeginForm("Index","Volleyball"))
{ <p>主队:  @Html.TextBox("SearchString")
客队:@Html.TextBox("teamb")
<input type="submit" value="查询" />
</p>
}
</p>
<table>
<tr>
<th>
@*@Html.DisplayNameFor(model => model.Hometeam)*@@Html.Encode("主队")
</th>
<th>
@* @Html.DisplayNameFor(model => model.Visitingteam)*@@Html.Encode("客队")
</th>
<th>
@*@Html.DisplayNameFor(model => model.One)*@@Html.Encode("第一局")
</th>
<th>
@*@Html.DisplayNameFor(model => model.Two)*@@Html.Encode("第二局")
</th>
<th>
@*@Html.DisplayNameFor(model => model.Three)*@@Html.Encode("第三局")
</th>
<th>
@*@Html.DisplayNameFor(model => model.Four)*@@Html.Encode("第四局")
</th>
<th>
@*@Html.DisplayNameFor(model => model.Five)*@@Html.Encode("第五局")
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Hometeam)
</td>
<td>
@Html.DisplayFor(modelItem => item.Visitingteam)
</td>
<td>
@Html.DisplayFor(modelItem => item.One)
</td>
<td>
@Html.DisplayFor(modelItem => item.Two)
</td>
<td>
@Html.DisplayFor(modelItem => item.Three)
</td>
<td>
@Html.DisplayFor(modelItem => item.Four)
</td>
<td>
@Html.DisplayFor(modelItem => item.Five)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}

</table>


创建比赛

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

//
// GET: /Score/

public ActionResult Index()
{

return View();
}
public ActionResult Score(Volleyball v)
{
ViewBag.hometeamscore = "0";
ViewBag.Visitingteamscore = "0";

ViewData.Add("Hometeam",v.Hometeam);
ViewData.Add("Visitingteam", v.Visitingteam);
return View();
}

public ActionResult Score2(Detailscore detailscore)
{

if (ModelState.IsValid)
{
db.Detailscores.Add(detailscore);
db.SaveChanges();
//return RedirectToAction("Index");
}
//List<String> team = (from t in db.Detailscores orderby t.ID descending  select t.Hometeam).ToList();
//ViewData.Add("Hometeam", team[0]);
//ViewData.Add("Visitingteam", team[2]);
return View("score");
}

}


创建比赛视图

@model MvcApplication1.Models.Volleyball

@{
ViewBag.Title = "Index";
}

<h2>输入队伍</h2>
@using (@Html.BeginForm("Score","Score"))
{  <fieldset>
<legend>volleyball</legend>
<div>
@Html.Encode("主方队")
@Html.EditorFor(Model => Model.Hometeam)
</div>
<div>
@Html.Encode("客方队")
@Html.EditorFor(Model => Model.Visitingteam)
</div>
<p><input type="submit" value="比赛开始"/></p>
</fieldset>

}


@model MvcApplication1.Models.Detailscore
@{
ViewBag.Title = "Score";
ViewBag.hometeamscore = "1";
ViewBag.Visitingteamscore = "0";

}
<style>
.div{float:left;margin:0 auto;text-align:center;width:100px;height:100px;}

</style>

<h2>计分板</h2>
@using (Html.BeginForm("Score2", "Score"))
{
@Html.ValidationSummary(true)
<div style="display:none;">
<div class="editor-label">
@Html.LabelFor(model => model.Hometeam)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Hometeam)
@Html.ValidationMessageFor(model => model.Hometeam)
</div>

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

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

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

<p id="home"></p>
@ViewBag.hometeamscore
<input onclick="funA()" type="submit" value="加分" />
</div>
<div class="div">
<p id="visiting"></p>
@ViewBag.Visitingteamscore
<input onclick="funB()" type="submit" value="加分" />
</div>
<script type="text/javascript">
var a = document.getElementById('Hometeam');
var b = document.getElementById('Visitingteam');

document.getElementById('home').innerHTML = a.value;
document.getElementById('visiting').innerHTML = b.value;

function funA() {

document.getElementById('Hometeamscore').value = 1;
document.getElementById('Visitingteamscore').value = "";

}
function funB() {
document.getElementById('Hometeamscore').value = "";
document.getElementById('Visitingteamscore').value = 1;
}
</script>


详细记录

public class DetailController : Controller
{
private DetailscoreDBContext db = new DetailscoreDBContext();

//
// GET: /Detail/

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


记录视图

@model IEnumerable<MvcApplication1.Models.Detailscore>

@{
ViewBag.Title = "Index";
}

<style type="text/css">
table, td, th {
border: thin solid black; border-collapse: collapse; padding: 5px;
background-color:gray; text-align: left; margin: 10px 0;
text-align:center;
}
</style>
<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@*@Html.DisplayNameFor(model => model.Hometeam)*@@Html.Encode("主队")
</th>
<th>
@*@Html.DisplayNameFor(model => model.Hometeamscore)*@@Html.Encode("主队得分")
</th>
<th>
@*@Html.DisplayNameFor(model => model.Visitingteam)*@@Html.Encode("客队")
</th>
<th>
@*@Html.DisplayNameFor(model => model.Visitingteamscore)*@@Html.Encode("客队得分")
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Hometeam)
</td>
<td>
@Html.DisplayFor(modelItem => item.Hometeamscore)
</td>
<td>
@Html.DisplayFor(modelItem => item.Visitingteam)
</td>
<td>
@Html.DisplayFor(modelItem => item.Visitingteamscore)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}

</table>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: