Asp.net-Mvc-3

我怎樣才能有一個使用 int 的 CheckBoxFor?

  • October 17, 2020

我需要幫助來建構 CheckBoxFor 以獲得int價值。

就像是:

@Html.CheckBoxForInt(m => m.foo.intValue)

如果沒有檢查,則應檢查intValue = 1

你為什麼不在你的模型中公開一個 bool 屬性來轉換成 int 或從 int 轉換?

像這樣的東西:

public bool BoolValue
{
   get { return IntValue == 1; }
   set { IntValue = value ? 1 : 0;}
}

public int IntValue { get; set; }

然後你可以用它來創建複選框

@Html.CheckBoxFor(m => m.foo.BoolValue)

出於某種原因,上面的響應給了我錯誤,但基於同樣的想法,我改變了這樣的程式碼:

public int IntValue { get; set; }

public bool BoolValue
{
   get { return IntValue == 1; }
   set { 
           if(value)
               IntValue = 1;
           else
               IntValue = 0;
   }
}

這對我有用。

引用自:https://stackoverflow.com/questions/13516113