How to save Html.CheckBox () state in ASP.NET MVC

I have two checkboxes in my MVC application, which are both logical and bit fields. "NotifyEmail" and "NotifySMS".

Whenever I send a message to the server and an error occurs, the state of the checkbox goes away, although I set the attempts.

+4
source share
8 answers

Why not so easy?

<%=Html.CheckBox("AgreeToRules", (Request.Form["AgreeToRules"] ?? string.Empty).Contains("true"))%> 
+7
source

Update: This is fixed in RC2.

This question along with this,

Html.Checkbox does not save its in ASP.net MVC ,

refers to a very undocumented ASPNET.MVC RC1 function. I searched for many hours to find a good answer, but there are very few.

There is an error , apparently, which prohibits checkboxes and radio buttons for maintaining its state from ModelState. As we also know, by now, these two controls are handled specifically by the HTML helpers.

The best I managed to come up with was to create my own ViewBinder:

From a very simple view:

 <h2>Keep checkbox value between posts</h2> <% using (Html.BeginForm("update", "checkbox")) {%> <p><%= Html.CheckBox("a") %></p> <p><%= Html.CheckBox("b") %></p> <p><%= Html.TextBox("dummy") %></p> <input type="submit" value="update" /> <% } %> 

Associated with an equally simple controller:

 public class CheckboxController : Controller { public ActionResult Index() { return View(); } public ActionResult Update() { var binder = new ViewBinder(ViewData, ValueProvider); binder.UpdateBooleanValues("a", "b"); binder.UpdateMissingValues(); return View("Index"); } } 

And a simple class to make it all work:

 internal class ViewBinder { private readonly IDictionary<string, ValueProviderResult> valueProvider; private readonly ViewDataDictionary viewData; public ViewBinder(ViewDataDictionary viewData, IDictionary<string, ValueProviderResult> valueProvider) { this.valueProvider = valueProvider; this.viewData = viewData; } public void UpdateMissingValues() { foreach (var key in valueProvider.Keys) { if (ValueIsMissing(key)) UpdateValue(key); } } public void UpdateBooleanValues(params string[] names) { foreach (var name in names) { UpdateValue(name, BooleanValueFor(name)); } } private bool BooleanValueFor(string name) { return valueProvider[name].AttemptedValue != "false"; } private bool ValueIsMissing(string key) { return viewData.ContainsKey(key) == false; } private void UpdateValue(string key) { var value = valueProvider[key].AttemptedValue; UpdateValue(key, value); } private void UpdateValue(string key, object value) { viewData[key] = value; } } 
+3
source

MVC does not have a ViewState, as WebForms does - this means that now you are responsible for maintaining these values.

This will require you to note whether a check box has been checked, and then check this box before the page is displayed in the browser in your view.

+2
source

Adam,

Assuming you understand that MVC does not include server-side functions for handling Asp.Net backlinks, if you need to send a message to a server that tells your application that this check box is selected, then it is probably best to do this using Javascript and the Ajax..Net MVC request includes the JQuery Javascript library, so doing this may be easier than you think. Below is a message on how to use checkboxes correctly in MVC.

Otherwise, make sure that MVC does not support post-back, and, as Andrew said above, also does not support the state of the Asp.Net view. However, you can go back to the old school way of representing the state and use a regular hidden HTML input element with a bit of javascript to maintain your state.

To get this said, you can take a minute to read the article . Many MVC frameworks assume that you will use the Post โ†’ Redirect โ†’ Get template to handle user input in your web forms. If you continue to use your current feedback and viewing state template, you may run into big problems in the future, such as the one you are currently trying to solve.

0
source

How do you specify your HTML checkbox? Binding will require a hidden input element in addition to the check box entry element. Html.Checkbox will handle this for you, or you can learn how it does it and do it yourself.

0
source

Using an HTML helper such as Html.CheckBox will be saved automatically in POST.

0
source

Andrei Rinea is partially right. But what I did was to use helpers and return to the previous page values โ€‹โ€‹in the DataClass (access to the model, etc.). It works well.

0
source

Others may find this solution useful:

Maintain state of dynamic checklist in ASP.NET MVC

0
source

All Articles