@ Html.RadioButtonFor boolean

时间:2013-03-08 04:47:53

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

我有一张表格:

@using (Html.BeginForm("Buy", "Keys", FormMethod.Post))
{
<div class="calc_steps">
    <div class="NumberedRow one">
        1.  @Html.DropDownListFor(model => model.PaymentSystem, Model.PaymentSystems, new { @class = "calcsteps_select styledselect" })
    </div>
    <div class="NumberedRow two">
        2. <div class="cnt">
            Укажите тип лицензии
            <div class="radio-jquery-ui">@Html.RadioButtonFor(model => model.IsElite, "false") <label>Обычная</label></div>
            <div class="radio-jquery-ui">@Html.RadioButtonFor(model => model.IsElite, "true") <label>Расширенная</label></div>
           </div>
    </div>
    <div class="NumberedRow three">
        3.
        <div class="cnt">
             Введите срок
            @Html.TextBoxFor(model => model.NumDays)
        </div>
    </div>
    <div class="itog">
        Итого: <span id="ïtogo">0 рублей 00 копеек</span>
    </div>
    <div>
        <input type="submit" value="Купить"/>
    </div>
</div>
}

型号:

public class BuyModel
{
    public string PaymentSystem;
    public bool IsElite;
    public int NumDays;
    public IEnumerable<SelectListItem> PaymentSystems;
}

动作:

    [HttpPost]
    public ActionResult Buy(BuyModel model)
    {
        return View("BuySummary", model);
    }

当我提交它时,model.IsElite总是假的(默认情况下)。我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

替换

public bool IsElite;

public bool IsElite {get;set;}

现在必须工作!

请记住绑定到properties而不是变量

答案 1 :(得分:0)

像这样。 ?  你需要为那个

设置一个属性
   public bool IsElite{get;set;};

然后尝试这样

@Html.RadioButtonFor(model => model.IsElite, "True", model.IsElite== true ? new { Checked = "checked" } : null)