gpt4 book ai didi

c# - actionresult 的不同结果

转载 作者:行者123 更新时间:2023-11-30 22:18:01 25 4
gpt4 key购买 nike

我正在使用 MVC 4 创建游戏。有一个犯罪功能,但我无法解决。我已经尝试了很多东西,但由于我是 MVC4 的新手,所以我无法弄明白。

我创建了一个单选按钮列表,每个不同的选项应该给出不同的结果。这是到目前为止的代码:

Controller :

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 天但没有结果。

谢谢

最佳答案

您是否在寻找 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>

关于c# - actionresult 的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16438703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com