我有一個窗體的ASP.NET MVC項目。在處理POST動詞的Action方法中,我有一個將表單數據綁定到我的模型實例的自定義IModelBinder
實現。如果有錯誤,我正在使用bindingContext.ModelState.SetAttemptedValue()
和bindingContext.ModelState.AddModelError()
將提交的值和錯誤消息保存到ModelState
。如何獲取CheckBox以在ASP.NET MVC中跨回發保持已檢查狀態?
這很好,我可以看到我的輸入控件上發生的預期行爲,這些行爲使用Html.TextBox()
(調用到Html.InputHelper()
)進行呈現。當我使用Html.CheckBox()
(它也調用通過Html.InputHelper()
)我的CheckBox的狀態是NOT輸出到th <input />
標記。
在我看來,似乎Html.InputHelper()
方法沒有使用ModelState的AttemptedValue作爲CheckBox類型的輸入字段。
這裏是從ASP.NET MVC Html.InputHelper()
method的代碼。
爲什麼CheckBox attemptValue沒有輸出到輸入標籤。有什麼我在這裏丟失,或者我需要通過檢查ModelState並自己設置標籤屬性來手動處理這種情況?
更新11/09
這裏是我使用的輸出複選框HtmlHelpers
電話:
<%= Html.CheckBox("IsDerived") %>
這裏是我使用的註冊嘗試值調用:
string isDerivedRequestValue = !string.IsNullOrEmpty(bindingContext.HttpContext.Request["IsDerived"]) ? bindingContext.HttpContext.Request.Form.GetValues("IsDerived") [0] : null;
bindingContext.ModelState.SetAttemptedValue("IsDerived", isDerivedRequestValue);