0

我正在创建一个以商店为特色的游戏。选择按钮时,必须根据按下的按钮返回一定的消息才能购买。我现在有下面的代码。我如何确保返回正确的消息(对于武器,武器消息,保护,保护消息)。

我一直在寻找一种方法来只选择一个单选按钮,但我相信它更简单。非常感谢您的帮助,谢谢!

控制器:

if (WeaponType != 0) {                      // buying weapon
            if (IsItemAllowed((int)WeaponType, gangster)) {
                if (gangster.Cash < WeaponPrice(WeaponType)) {
                    ViewBag.Message = "You don't have enough cash to buy this weapon";
                    return View();
                }
                gangster.Cash -= WeaponPrice(WeaponType);
                gangster.Weapon++;
                ViewBag.Message = "You have succesfully purchased this weapon";
            } else {
                ViewBag.Message = "You can't buy this weapon yet";
            }
        } else if (ProtectionType != 0) { // buying protection
            if (IsItemAllowed((int)ProtectionType, gangster)) {
                if (gangster.Cash < ProtectionPrice(ProtectionType)) {
                    ViewBag.Message = "You don't have enough cash to buy this protection";
                    return View();
                }
                gangster.Cash -= ProtectionPrice(ProtectionType);
                gangster.Protection++;
                ViewBag.Message = "You have succesfully purchased this protection";
            } else {
                ViewBag.Message = "You can't buy this protection yet";
            }
        }

看法:

<div class="row-fluid" style="margin-top: 20px">
@using (Html.BeginForm("Index")) {
    @Html.Button("WeaponType", LocalStoreModel.StoreWeapon.Weapon01, false)@: .38 S&W Model 12<br>
    @Html.RadioButton("WeaponType", LocalStoreModel.StoreWeapon.Weapon02, false)@: .45 M1911<br>
    @Html.RadioButton("WeaponType", LocalStoreModel.StoreWeapon.Weapon03, false)@: Remington Model 11<br>
    @Html.RadioButton("WeaponType", LocalStoreModel.StoreWeapon.Weapon04, false)@: .351 Winchester SL<br>
    @Html.RadioButton("WeaponType", LocalStoreModel.StoreWeapon.Weapon05, false)@: Thompson Model 1921 <br>
    @Html.RadioButton("WeaponType", LocalStoreModel.StoreWeapon.Weapon06, false)@: Browning Automatic Rifle <br>
    @Html.RadioButton("WeaponType", LocalStoreModel.StoreWeapon.Weapon07, false)@: Browning M2<br>
    <br>
    <input class="btn btn-large btn-primary" type="submit" value="Buy Weapon" />
    <br>
    @Html.RadioButton("ProtectionType", LocalStoreModel.StoreProtection.Protection01, false)@: Layered Fabric Vest<br>
    @Html.RadioButton("ProtectionType", LocalStoreModel.StoreProtection.Protection02, false)@: Metal Plate Vest<br>
    @Html.RadioButton("ProtectionType", LocalStoreModel.StoreProtection.Protection03, false)@: Ceramic Plate Vest<br>
    @Html.RadioButton("ProtectionType", LocalStoreModel.StoreProtection.Protection04, false)@: Armoured T-Ford<br>
    @Html.RadioButton("ProtectionType", LocalStoreModel.StoreProtection.Protection05, false)@: Armored Car (Custom)<br>
    <br>
    <input class="btn btn-large btn-primary" type="submit" value="Buy Protection" />
    <br>
}

4

1 回答 1

0

我相信你需要为你的助手添加一个id标签。RadioButton如果您查看帮助程序的MSDN 参考,您可以看到如何将 HTML 属性添加到您的单选按钮。分组在一起的所有单选按钮的 id 应该相同。

例如,您的前两个将如下所示:

@Html.Button("WeaponType", LocalStoreModel.StoreWeapon.Weapon01, false, new {id="weaponType"})@: .38 S&W Model 12<br>
@Html.RadioButton("WeaponType", LocalStoreModel.StoreWeapon.Weapon02, false, new {id="weaponType"})@: .45 M1911<br>
于 2013-05-17T15:21:35.523 回答