【问题标题】:@Html.RadioButtonFor boolean@Html.RadioButtonFor 布尔值
【发布时间】:2013-03-08 04:47:53
【问题描述】:

我有一个表格:

@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 始终为 false(默认情况下)。我在这里做错了什么?

【问题讨论】:

  • 看起来很完美。那总是sux。我怀疑这很好,但请分享您的模型和操作方法。
  • 我更新了我的帖子。感谢您的宝贵时间!
  • 你应该绑定到属性而不是变量

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


【解决方案1】:

替换

public bool IsElite;

与

public bool IsElite {get;set;}

它现在必须工作!

记得绑定properties而不是变量

【讨论】:

  • 天哪!谢谢你!我已经用 Mvc 编码了很长时间,从来没有犯过这样的错误......也许是时候睡觉了 o.O
【解决方案2】:

像这样。 ? 你需要为它设置一个属性

   public bool IsElite{get;set;};

那就这样试试吧

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-08
    • 2011-04-13
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2018-01-24
    相关资源
    最近更新 更多