2013-03-08 35 views
6

Ive得到了一種形式:@ Html.RadioButtonFor布爾

@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始終是假的(默認情況下)。我在這裏做錯了什麼?

+0

看起來很完美。總是sux。我懷疑這很好,但分享你的模型和行動方法。 – 2013-03-08 04:56:39

+0

我更新了我的文章。感謝您的時間! – CodeDemen 2013-03-08 05:02:33

+1

你應該綁定到屬性,而不是變量 – 2013-03-08 05:04:41

回答

1

更換

public bool IsElite; 

public bool IsElite {get;set;} 

它現在的工作!

記得要綁定到properties而不是變量

+1

Omg!謝謝!我已經用Mvc編碼了大量的時間,並且從未犯過這樣的錯誤......也許是時候去睡覺了o.O – CodeDemen 2013-03-08 05:16:23

0

這樣子。 ? 你需要設置一個屬性,用於

public bool IsElite{get;set;}; 

然後再嘗試這樣

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

不,我的意思是它總是發佈一個模型「False」值 – CodeDemen 2013-03-08 05:05:00