返回2个radiobutton选项的正确消息

时间:2013-05-17 15:01:15

标签: c# asp.net-mvc-4

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

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

控制器:

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>
}

1 个答案:

答案 0 :(得分:0)

我认为您需要在id帮助中添加RadioButton标记。如果查看助手的MSDN reference,可以看到如何将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>