2

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

谢谢

4

2 回答 2

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>填充的表单数据库。ACrime应该是这样的:

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>
于 2013-05-08T11:16:58.527 回答
1

您的数据库中应该有一个Crime表,这样您就可以存储针对每次犯罪的支出。

另一方面,将所有这些单选按钮更改为下拉列表不是更好吗?

于 2013-05-08T10:52:10.350 回答