簡體   English   中英

為什么Html.Checkbox(“Visible”)在ASP.NET MVC 2中返回“true,false”?

[英]Why Html.Checkbox(“Visible”) returns “true, false” in ASP.NET MVC 2?

我正在使用Html.Checkbox("Visible")向用戶顯示一個復選框。 在回發中, FormCollection["Visible"]值為“true,false”。 為什么?

在視圖中:

<td>                
    <%: Html.CheckBox("Visible") %>
</td>

在控制器中:

 adslService.Visible = bool.Parse(collection["Visible"]);

這是因為CheckBox幫助器生成了一個與復選框同名的附加隱藏字段(您可以通過瀏覽生成的源代碼來查看它):

<input checked="checked" id="Visible" name="Visible" type="checkbox" value="true" />
<input name="Visible" type="hidden" value="false" />

因此,當您提交表單時,這兩個值都將發送到控制器操作。 這是一個直接來自ASP.NET MVC源代碼的注釋,解釋了這個額外隱藏字段背后的原因:

if (inputType == InputType.CheckBox) {
    // Render an additional <input type="hidden".../> for checkboxes. This
    // addresses scenarios where unchecked checkboxes are not sent in the request.
    // Sending a hidden input makes it possible to know that the checkbox was present
    // on the page when the request was submitted.
    ...

我建議您使用視圖模型作為操作參數或直接標量類型,而不是使用FormCollection ,並將解析的麻煩留給默認的模型綁定器:

public ActionResult SomeAction(bool visible)
{
    ...
}

我最近處理了這個問題,並提出了一種繞過MVC綁定並在查詢字符串上使用Contains(“true”)的方法。 沒有其他任何對我有用。

如果人們堅持其他答案,那么這對我有用 - http://websitesorcery.com/post/2012/03/19/CheckBox-Issue-with-MVC-3-WebGrid-Paging-Sorting.aspx

如果您希望/需要使用FormCollection ,而不是檢查truefalse ,請檢查true,falsefalse

例如,而不是這個

adslService.Visible = bool.Parse(collection["Visible"]);

做這個

adslService.Visible = bool.Parse(collection["Visible"] != "false");

我在MVC 4中遇到了同樣的問題,

這個解決方案對我有用。

我的(表格)查看:

@Html.EditorFor(m =>  m.SomeBoolValue)

用於在CheckBox為true時清空重復的“true,false”值

我將CheckBox參數存儲在一個數組中:

var arrParams = Request.Form["SomeBoolValue"].Split(',');

抓住第一個元素:

string sParam = arrParams[0].ToString();

解析它:

bool BoolValue = bool.Parse(sParam);

收下:

MyObject.SomeBoolValue = BoolValue;

我遇到過同樣的問題。 我用以下組合修復它(在Darin Dimitrov的幫助下 - 謝謝):

視圖:

<label for="optIn"><%=Html.CheckBox("optIn", ViewData["OptIn"])%>

控制器:

public ActionResult Index()
{
   ViewData["Optin"] = True;
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form, bool OptIn )
{        
    ViewData["Optin"] = OptIn;
}

這里是控件的來源,有和沒有實際選中的復選框(供參考):

經過:

<input Length="4" checked="checked" id="optIn" name="optIn" type="checkbox" value="true" /><input name="optIn" type="hidden" value="false" />

未選中:

<input Length="4" id="optIn" name="optIn" type="checkbox" value="true" /><input name="optIn" type="hidden" value="false" />

所以,這就是我解釋行為的方式:

如果取消選中該復選框,HTML將不會回發字段值,但如果是,則會回發。 幫助器在復選框控件之后附加隱藏字段(值為'False')。 如果選中該復選框,則源顯示“checked ='checked'”如果未選中,則不顯示。 因此,如果checked = checked,則將true傳遞回控制器。 如果未選中此框,則不會傳回控件,因此名為相同的隱藏字段將接管並傳回false。 這樣你就有兩個條件。 奇怪,但它的工作原理。 我希望這有幫助。

MVC Entension for Html.Checkbox的默認行為會生成一個隱藏字段以及要發布的字段。 在我的情況下,因為我添加到頁面的自定義復選框不像Html.CheckBoxFor那樣綁定模型,所以我決定將其設置為簡單的html復選框。

 <input type="checkbox" name="WithAttachment" checked="checked"/> Include Attachments 

這可能聽起來很愚蠢,但可以節省一些人的時間,而不是在工作后跑步。 這將阻止使用MVC擴展創建復選框

我已經有一個靜態函數,我在任何地方使用它來處理布爾類型文本並返回true或false作為布爾值所以我只是在那里處理它

            if (sBool.StartsWith("true,")) bReturn = true;//cater for mvc checkboxes

            else if (sBool == "true") bReturn = true;

            else if (sBool == "t") bReturn = true;//t-f

            else if (sBool == "1") bReturn = true;//1-0

            else if (sBool == "yes") bReturn = true;//yes-no

            else if (sBool == "y") bReturn = true;//y-n

            else if (sBool == "on") bReturn = true;//on-off

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM