<%=html.checkbox("Name", boolean)%> checkbox doesn't get checked - asp.net-mvc

Why is the checkbox not checked with this code?
<td><%=Html.CheckBox("ItemIsActive", item.ItemIsActive)%></td>
Item.ItemIsActive is of type boolean and set to true?
When i do this, it shows "true" in the view
<td><%=item.ItemIsActive%></td>

I had this same issue until I changed my boolean field to a non-nullable field. Make sure your Item.IsActive field is declared like:
bool ItemIsActive {..}
instead of
bool? ItemIsActive {..}
If you are using Linq2Sql make sure ItemIsActive is not nullable in your table.

I had the same problem as well (using ASP.NET MVC).
Actually I had a form with two checkboxes, one where the boolean is nullable and the other non-nullable.
In the case of the non-nullable the checkbox was checked when the value from the database was true. In the case of the nullable I couldn't get it checked using Html.CheckBox.
I solved it by converting the value from the model to non-nullable boolean.
<td><%=Html.CheckBox("ItemIsActive", item.ItemIsActive == null ?
false :
(bool)item.ItemIsActive) %></td>

I've played with this for around 30 minutes now and I have tried the following;
1) Create a checkbox and set to true
2) Create a variable, set to true and use it as the checked flag
3) Create two checkboxes with the same id setting one to false the other to true
All of the above works fine so the (only) thing I can think of is that you may have some css or javascript setting checkboxes back to false. This is easy to do if you are using jQuery.
The fact that there are so few replies to this question seems to indicate that others can't seem to replicate your issue either.
Like I said, I'd be checking my CSS and any Javascript/jQuery I have on the page, partial controls, master etc.

If you have a viewdata item called ItemIsActive, or if your view is tied to a specific model that has a property called that, it will use that over what you specify.
To see if this might be it try changing it to this:
<td><%=Html.CheckBox("ItemIsActive_test", item.ItemIsActive)%></td>
If that works then I would bet something in the viewdata, or in the model is overwriting what item.ItemIsActive contains

Related

Missing View Columns that cause nulls to be saved when using Model Binding

Just need some clarification.
If I have a form with fields that should not be modified by the user then I need to use the HiddenFor helper to pass those values through otherwise those values will be missing and Model Binding will ensure Nulls are stored for those Field Values.
Of course this seems over zealous of the Model Binder, and one would think that if a field did not exist at all in the View(response stream) then that field would be left untouched. Obviously an EditFor field with a value of "" is different and that value should be null or "".
The only other approach I have come across is to use objects mappers such as Automapper to ensure nulls are ignored.
At the moment I will just use hidden fields to pass the values through.
Thoughts on the above appreciated or rather what is the recommended method for dealing with this issue?
Thanks.

Multiple dropdowns from one selectlist asp.net mvc

I just ran into an issue that I have never noticed before and I want to know if this is something isolated to my current project for some reason or if this is an issue that I need to panic about and check every one of my applications and correct right away.
I have two drop downs on the same page using the same select list.
ex. HomeCity and CurrentCity
both populated from the same list of cities. Therefore in code both populated from the same IEnumerable<selectListItem>
It turns out that if the second value on the page is null (in the view model) then instead of defaulting to the optionLabel it defaults to whatever the first value is.
This issue is much exasperated when (as I often do) you try to cache the SelectList. Not only is the next item on the same page set to the wrong value, but the selected item gets changed in the cache (which is so strange because you would imagine that the cache serializes the values and give you a copy.
So my question is twofold.
is this the usual functionality or did I trigger this weird behavior somehow?
What is the recommended way to avoid this? Would I need to do a deep clone of my select list between dropdowns?

I don't get the CheckboxFor html helper in asp.net mvc

I don't why the Html.CheckBoxFor(x => x.IsChecked) helper was implemented. Why does it force you to have to use a bool value?
From what I seen the regular html input can have a "value" of any string. So why does the html helper limit you?
I am having a problem right now where I would love to change the "value" to store my GUID but since it only takes in a bool I can't do this.
I see other people make a HiddenFor() to get around this but I just find this weird.
It's because usually a checkbox has 2 states: checked and unchecked. Which is perfectly modeled by a boolean. Now I understand your pain and agree with you that this could be a bit of a limitation because the underlying <input> HTML element allows for potentially any type. But this limitation is very easily workarounded by simply adding a boolean property to your view model and then bind the CheckBoxFor helper to this property.

Removing Required Attribute from Class but MVC3 still won't post the form without a value in the text box

I have a class. At one point, I had set the properties of the class to [Required] using System.ComponentModel....
Okay, then I realized this was not needed. I have removed the required property but when I try to submit the form to an ActionResult the form does NOT post and still is trying to enforce the TextBoxFor(theModelProperty) to be populated.
I have deleted the "obj" folder, the "bin" folder, and also "Cleaned" the solutions. Still NO resolution.
I don't want to do a stupid workaround, I would like to do things correctly. Any idea why this occurs?
You have two options:
Set property as nullable,
Turn off required attribute for value
types (see this answer)
If you have value type properties. Client validation will always generate required validations. If you don't want required validation for value types make them nullable.

ASP.NET MVC 2 RC2 Model Binding with NVARCHAR NOT NULL column

PLEASE NOTE: I've answered my own question with a link to an answer to a similar question. I'll accept that answer once I'm allowed to (unless anyone comes up with a better answer meantime).
I have a database column defined as NVARCHAR(1000) NOT NULL DEFAULT(N'') - in other words, a non-nullable text column with a default value of blank.
I have a model class generated by the Linq-to-SQL Classes designer, which correctly identifies the property as not nullable.
I have a TextAreaFor in my view for that property. I'm using UpdateModel in my controller to fetch the value from the form and populate the model object.
If I view the web page and leave the text area blank, UpdateModel insists on setting the property to NULL instead of empty string. (Even if I set the value to blank in code prior to calling UpdateModel, it still overwrites that with NULL). Which, of course, causes the subsequent database update to fail.
I could check all such properties for NULL after calling UpdateModel, but that seems ridiculous - surely there must be a better way?
Please don't tell me I need a custom model binder for such a simple scenario...!
Might be duplicate or something in the line of this:
MVC binding form data problem
I fear custom model binder will be necessary. ;)
You might want to use a partial class implementation of your entity that implements the on property changed handler for that particular property. When you detect that the property has been changed to NULL, simply change it to string.Empty. That way anytime NULL is assigned to the property it gets reset to the empty string.

Resources