我正在使用 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 天没有结果。
谢谢