开发者

Bind CheckBoxFor to bool?

How to bind nullable bool to checkbox in MVC 2. I try with this code in view:

<%: Html.CheckBoxFor(model =>  开发者_运维知识库model.Communication.Before)%>

But show me compilation error.

Thanks in advance.


I know about this issue. You can try to use this workaround:

Create new property called Before in yours ViewModel:

public class YoursViewModel 
{
    public Communication Communication { get; set; }

    public bool Before
    {
        get
        {
            bool result;
            if (this.Communication.Before.HasValue)
            {
                result = (bool)this.Communication.Before.Value;
            }
            else
            {
                result = false;
            }

            return result;
        }
        set
        {
            this.Communication.Before = value;
        }
    }
}

Also you have to be careful for Communication property this have to be instanced before use. For example when you initialize ViewModel in controller you also have to initialize this property.

ControllerAction()
{
  YoursViewModel model = ViewModelFactory.CreateViewModel<YoursViewModel >("");
  model.Communication = new Communication ();
  return View(model);
}

Thanks Ivan Baev


A checkbox can have two states: ckecked/uncheked, true/false, 1/0. So trying to bind a checkbox to a property that could potentially have three states doesn't really fit the picture. I would recommend you adapting your view model so that it uses a non nullable boolean property. If in your domain model you have a nullable boolean which you cannot change you could do this in the mapping layer between your domain model and view model.


One way to bind Checkbox in MVC View

With EF database first, boolean (bit) field in the database produces a nullable bool? Property in the generated class. For demo I have a table named Dude with the fields

  • Id uniqueidentifier
  • Name varchar(50)
  • IsAwesome bit

The following class is generated by EF:

namespace NullableEfDemo
{
 using System;
 public partial class Dude
 {
    public System.Guid Id { get; set; }
    public string Name { get; set; }
    public Nullable<bool> IsAwesome { get; set; }
 }
}

To be able to bind IsAwesome to checkbox I simply extend the class Dude. This is to avoid editing the generated class, if I need to refresh it. So I added a code file DudePartial.cs to my project (the name is irrelevant). Don’t forget to declare or using the project namespace:

namespace NullableEfDemo
{
 partial class Dude
 {
    public bool Awesome
    {
        get { return IsAwesome ?? false; }
        set { IsAwesome = value; }
    }
  }
}

This declares a new property Awesome of type bool that can be bound to the checkbox in the Edit view

@Html.CheckBoxFor(model => model.Awesome, new { @class = "control-label" })

In the HttpPost I’m binding the models Awesome property instead of IsAwesome.

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Name,Awesome")] Dude dude)
    {…
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜