I have one int field in databse which represents IsThumbnail field which will have vlaue 0,1. I am aware about It should be bool/bit but for some reason I can't change that.
Now I want to remove the validation that says "The field IsThumb must be a number." and need to save 1 to db if the check box is checked else 0.
Are you mapping your db model directly on UI, if yes, then add an extra property for boolean and in get/set can explicitly set the value of your db field.
Now when I check the check box and click submit button it is showing me "The field Display Thumb must be a number." validation error. I need to save 1 to db if check box checked and o if check box
not checked.
kyuti
Contributor
2027 Points
549 Posts
Storing Int value from check box
Feb 16, 2013 04:40 PM|LINK
Hello All,
I have one int field in databse which represents IsThumbnail field which will have vlaue 0,1. I am aware about It should be bool/bit but for some reason I can't change that.
Now I want to remove the validation that says "The field IsThumb must be a number." and need to save 1 to db if the check box is checked else 0.
Please suggest how can I achive that.
Thanks in advance.
Kalpana Patel
Xequence
Contributor
4303 Points
1528 Posts
Re: Storing Int value from check box
Feb 16, 2013 04:46 PM|LINK
you can wirein a custom validator attribute on the property to check using ValidationAttribute.
part of the validationContext..
http://www.codeproject.com/Articles/260177/Custom-Validation-Attribute-in-ASP-NET-MVC3
http://weblogs.asp.net/scottgu/archive/2010/12/10/class-level-model-validation-with-ef-code-first-and-asp-net-mvc-3.aspx
hth
Credentials
CPrakash82
All-Star
18154 Points
2831 Posts
Re: Storing Int value from check box
Feb 16, 2013 04:49 PM|LINK
Why not simply use Convert.ToInt32 to convert your checkbox provided boolean value to int type?
kyuti
Contributor
2027 Points
549 Posts
Re: Storing Int value from check box
Feb 16, 2013 04:58 PM|LINK
Yes I can convert it but on clicking save it is showing "The field IsThumb must be a number." validation message. Please suggest.
Kalpana Patel
kyuti
Contributor
2027 Points
549 Posts
Re: Storing Int value from check box
Feb 16, 2013 04:59 PM|LINK
I have implemented custom validator but it is still showing the message of validation. Please suggest.
Kalpana Patel
CPrakash82
All-Star
18154 Points
2831 Posts
Re: Storing Int value from check box
Feb 16, 2013 05:01 PM|LINK
Are you mapping your db model directly on UI, if yes, then add an extra property for boolean and in get/set can explicitly set the value of your db field.
Xequence
Contributor
4303 Points
1528 Posts
Re: Storing Int value from check box
Feb 16, 2013 05:04 PM|LINK
some code as to how you are resolving your modelstate errors...
Credentials
kyuti
Contributor
2027 Points
549 Posts
Re: Storing Int value from check box
Feb 16, 2013 05:22 PM|LINK
Hello,
This is my model
public Nullable<int> displayThumb { get; set; }
And this my Rozor on view
@Html.CheckBox("displayThumb", Convert.ToInt32(Model.displayThumb) == 0 ? false : true)
@Html.ValidationMessageFor(model => model.displayThumb)
<p>
<input type="submit" value="Save" class="button" />
</p>
Now when I check the check box and click submit button it is showing me "The field Display Thumb must be a number." validation error. I need to save 1 to db if check box checked and o if check box not checked.
Kalpana Patel
Xequence
Contributor
4303 Points
1528 Posts
Re: Storing Int value from check box
Feb 16, 2013 05:23 PM|LINK
checkboxfor(f => f.displayThumb)?
Credentials
kyuti
Contributor
2027 Points
549 Posts
Re: Storing Int value from check box
Feb 16, 2013 05:25 PM|LINK
No that won't work as the field is nullable int.
Kalpana Patel