I am getting problem with binding a bit datatype to check box (MVC 2 + MSSQL 2005 + EF).
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/EditForm.Master" Inherits="System.Web.Mvc.ViewPage<Framework.Models.job>" %>
...
<label>
Is Hot
</label>
<%=Html.CheckBoxFor(model=>model.isHot) %>
I've already used an attribute to setting isHot field to false whenever it is NULL. It's still generate error message.
Below is my custom attribulte
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class BooleanAttribute : ValidationAttribute
{
public bool Value
{
get;
set;
}
public override bool IsValid(object value)
{
return value != null && value is bool && (bool)value == Value;
}
}
Then I created an partial call to use this attribute
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by truongthang on May 10, 2010 10:26 AM
truongthang
Member
18 Points
41 Posts
Binding bool value to checkbox in MVC 2
May 10, 2010 03:53 AM|LINK
I am getting problem with binding a bit datatype to check box (MVC 2 + MSSQL 2005 + EF).
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/EditForm.Master" Inherits="System.Web.Mvc.ViewPage<Framework.Models.job>" %> ... <label> Is Hot </label> <%=Html.CheckBoxFor(model=>model.isHot) %>I've already used an attribute to setting isHot field to false whenever it is NULL. It's still generate error message.
Below is my custom attribulte
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class BooleanAttribute : ValidationAttribute { public bool Value { get; set; } public override bool IsValid(object value) { return value != null && value is bool && (bool)value == Value; } }Then I created an partial call to use this attribute
Could anybody suggest me how can I handle this?
Thanks in advance.
ASP.NET MVC "ASP.NET MVC" asp.mvc modelbinder Asp .net mvc validation complex logic asp .net mvc dataannotation asp.net mvc 2 asp.mvc modelbinder validation "ASP.NET MVC 2"
Email: truongthang_ad@yahoo.com
ignatandrei
All-Star
137549 Points
22117 Posts
Moderator
MVP
Re: Binding bool value to checkbox in MVC 2
May 10, 2010 04:17 AM|LINK
I do not know why you need an attribute when it's easier to do it in code ...But anyway, you can do the following
<%=Html.CheckBoxFor(model=>model.isHot.Value) %>
mohd786hussa...
Contributor
4329 Points
878 Posts
Re: Binding bool value to checkbox in MVC 2
May 10, 2010 05:27 AM|LINK
Hi,
you can try the following :
<%=Html.CheckBoxFor(model=>model.isHot.HasValue?model.isHot.Value:false) %>
Mohammad Hussain
Web design, Logo design & Asp.net development
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Binding bool value to checkbox in MVC 2
May 10, 2010 06:42 AM|LINK
<%= Html.CheckBoxFor(model => model.isHot ?? false) %>
truongthang
Member
18 Points
41 Posts
Re: Binding bool value to checkbox in MVC 2
May 10, 2010 06:54 AM|LINK
<%=Html.CheckBoxFor(model=>model.isHot.HasValue?model.isHot.Value:false) %>
I've tried this but it raised error on compile time.
Then I'm trying to use
<%= Html.CheckBoxFor(model => model.isHot ?? false) %>
It passed combine time but raised error on runtime because model.isHot is null.
Email: truongthang_ad@yahoo.com
truongthang
Member
18 Points
41 Posts
Re: Binding bool value to checkbox in MVC 2
May 10, 2010 07:04 AM|LINK
<%=Html.CheckBoxFor(model=>model.isHot.Value) %>
It works well only isHot is not null in database otherwise it's still get error whenever isHot value is null. Below is my code to asign true or false.
public ActionResult Edit(int Id, FormCollection frmCollection) { ... if (!String.IsNullOrEmpty(frmCollection.GetValues("isHot.Value")[0])) { bool isHot= Convert.ToBoolean(frmCollection.GetValues("isHot.Value")[0]); obj.isHot= isHot; } }Email: truongthang_ad@yahoo.com
ignatandrei
All-Star
137549 Points
22117 Posts
Moderator
MVP
Re: Binding bool value to checkbox in MVC 2
May 10, 2010 07:22 AM|LINK
right - you've said in a previous post
So ... I do not understand where is the problem ...maybe your attribute isn't working well ?
imran_ku07
All-Star
45847 Points
7710 Posts
MVP
Re: Binding bool value to checkbox in MVC 2
May 10, 2010 09:20 AM|LINK
<%
bool isHot=false;
if(model.isHot.HasValue)
isHot=model.isHot.Value;
Response.Write(Html.CheckBoxFor(model=>isHot))
%>
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
truongthang
Member
18 Points
41 Posts
Re: Binding bool value to checkbox in MVC 2
May 10, 2010 10:26 AM|LINK
I've tried your code . That's work properly. Thanks so much.
Email: truongthang_ad@yahoo.com
shabeer.ak
Member
2 Points
1 Post
Re: Binding bool value to checkbox in MVC 2
Jun 17, 2011 11:18 PM|LINK
this single line of code would do the trick, even when the value is NULL.
<%=Html.CheckBox("isHot", Model.isHot ?? false) %>