我正在使用MVC 4创建一个游戏。有一个犯罪功能,我无法解决它。我已经尝试了很多东西,但由于我是MVC4的新手,我无法理解它。
我创建了一个单选按钮列表,每个不同的选项应该会给出不同的结果。这是到目前为止的代码:
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiveGangs.Models;
namespace FiveGangs.Controllers.Game {
[Authorize]
public class CrimesController : Controller {
//
// GET: /Crimes/
[HttpGet]
public ActionResult Index() {
return View();
}
//
// POST: /Crimes/
[HttpPost]
public ActionResult Index(LocalCrimeModel.Crime crimeType) {
//1 is de beste crime, 10 de slechste
ViewBag.Message = "You commited the crime!";
var db = new FGEntities();
var g = db.UserGangster.FirstOrDefault(p => p.UserProfile.UserName == User.Identity.Name);
g.GansterStatistic.CrimesSuccess++;
g.Experience += 1000;
g.Cash += 100;
db.SaveChanges();
return View();
}
}
}
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FiveGangs.Models
{
public class LocalCrimeModel
{
public enum Crime
{
Crime01 = 1,
Crime02 = 2,
Crime03 = 3,
Crime04 = 4,
Crime05 = 5,
Crime06 = 6,
Crime07 = 7,
Crime08 = 8,
Crime09 = 9,
Crime10 = 10,
}
public Crime SelectedCrime { get; set; }
}
}
查看:
@using FiveGangs.Models
@model FiveGangs.Models.LocalCrimeModel
<!DOCTYPE html>
<html>
<head>
<title>Crimes</title>
</head>
<body>
<div>
<h2>Crimes</h2>
@if (ViewBag.Message != null) {
<p class="warning">@ViewBag.Message</p>
}
@using (Html.BeginForm("Index")) {
@Html.RadioButton("crimetype", LocalCrimeModel.Crime.Crime01, false)@: Rob the local bank<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime02, false)@: Rob the local casino<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime03, false)@: Rob a jewelry store<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime04, false)@: Rob the local café<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime05, false)@: Steal casino chips from the local casino<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime06, false)@: Rob the local theatre<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime07, false)@: Rob the local supermarket<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime08, false)@: Rob a laundry service<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime09, false)@: Empty a parking meter<br>
@Html.RadioButton("crimeType", LocalCrimeModel.Crime.Crime10, false)@: Pickpocket someone<br /><br />
<input class="btn btn-large btn-primary" type="submit" value="Commit crime!" />
}
</div>
</body>
</html>
到目前为止,我可以成功执行犯罪,并将经验和资金添加到数据库中。我坚持改变每次犯罪的“赔付”。任何帮助表示赞赏,我一直在谷歌搜索2天没有结果。
由于
答案 0 :(得分:2)
您在寻找switch
语言构造吗?
你可以写:
switch (crimeType)
{
case LocalCrimeModel.Crime.Crime01:
g.Experience += 1000;
g.Cash += 100;
break;
case LocalCrimeModel.Crime.Crime02:
g.Experience += 2000;
g.Cash += 200;
break;
case LocalCrimeModel.Crime.Crime03:
g.Experience += 3000;
g.Cash += 300;
break;
case LocalCrimeModel.Crime.Crime04:
g.Experience += 4000;
g.Cash += 400;
break;
...
default:
break;
}
你不能吗?
修改强>:
另一方面,这种硬编码逻辑实际上不是可扩展的,犯罪不应该是enum
,而应该是List<Crime>
填充的表单数据库。 Crime
应该是:
public class LocalCrimeModel
{
public class Crime
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Cash { get; set; }
public int Experience { get; set; }
}
public string Message { get; set; }
public IList<Crime> Crimes { get; set; }
public int SelectedCrimeID { get; set; }
public Crime SelectedCrime
{
get
{
if (this.Crimes != null)
{
return this.Crimes.FirstOrDefault(o => o.ID == this.SelectedCrimeID);
}
else
{
return null;
}
}
}
}
CrimesController就像:
public class CrimesController : Controller
{
public ActionResult Index()
{
var model = new LocalCrimeModel();
model.Crimes = GetCrimes(); // THIS SHOULD BE A RESULT OF A DATABASE QUERY
return View(model);
}
[HttpPost]
public ActionResult Index(LocalCrimeModel model)
{
if (model != null)
{
model = new LocalCrimeModel(); // just to be sure
}
model.Crimes = GetCrimes(); // USE THE SAME QUERY HERE
if (model != null && model.SelectedCrime != null)
{
var db = new FGEntities();
var g = db.UserGangster.FirstOrDefault(p => p.UserProfile.UserName == User.Identity.Name);
g.GansterStatistic.CrimesSuccess++;
g.Experience += model.SelectedCrime.Experience;
g.Cash += model.SelectedCrime.Cash;
db.SaveChanges();
model.Message = "You commited the crime!";
}
else
{
model.Message = "Crime does not exist!";
}
return View(model);
}
}
您的观点也可能如下:
@using FiveGangs.Models
@model FiveGangs.Models.LocalCrimeModel
<!DOCTYPE html>
<html>
<head>
<title>Crimes</title>
</head>
<body>
<div>
<h2>Crimes</h2>
@if (model.Message != null)
{
<p class="warning">@model.Message</p>
}
@using (Html.BeginForm())
{
foreach (var crime in Model.Crimes)
{
<text>@Html.RadioButton("SelectedCrimeID", crime.ID, false): @crime.Name<br /></text>
}
<br />
<input class="btn btn-large btn-primary" type="submit" value="Commit crime!" />
}
</div>
</body>
</html>
答案 1 :(得分:1)
您的数据库中应该有一个Crime
表,这样您就可以针对每种犯罪存储付款。
另一方面,将所有这些单选按钮更改为下拉列表是不是更好。