0

I am working on a ASP.NET MVC 4 project. I use Entity Framework 5 and Code First workflow. Here is part of my entity:

public class News
{
    //Other properties...
    public byte IsShortNews { get; set; }

}

When I edit an existing news I want to render the checkbox. The compiler complains about not being able to parse byte to bool so I tried this :

<li>
  @Html.CheckBoxFor(m => (m.IsShortNews == 1? true : false))
  @Html.LabelFor(m => m.IsShortNews, new { @class = "checkbox" })
</li>

And I get this error :

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Which I'm not sure what exactly wants to tell me but it seems that I won't solve my problem so easily.

Searching the web I found this solution:

   public byte IsShortNews {get;set;}
   [NotMapped]
   public bool IsShortNewsBool 
   {
       get { return IsShortNews > 0; }
       set { IsShortNews = value ? 1 : 0; }
   }

but I have little experience before with NotMapped properties and I don't know if it was specifically for that application but we decided that it's better to get rid of them from the project so I decided to look for another solution.

In the view where I create news I use ViewModel where IsShortNews is declared as bool and then in my controller I just news.IsShortNews = model.IsShortNews? (byte)1 : (byte)0; but I'm not sure if using a viewmodel here is a good choice. Since it's an existing record in the database and I have to use the entity to get it :News model = unitOfWork.NewsRepository.GetById(Id); and then have to create another object with my viewmodel type - NewsModel vModel = new NewsModel(); then copy the values from the one object to the other in order to pass a model to the view and... it seems like a lot of work for something like this.

1
  • 1
    Why are you using a byte instead of a bool if your values are 1 or 0? Or am I missing something? Commented Nov 29, 2013 at 21:36

1 Answer 1

3

For boolean values, you need to make your database columm type a bit and your class property a bool and you won't need to do any extra work to display a checkbox.

public class News
{
    //Other properties...
    public bool IsShortNews { get; set; }
}

HTML

<li>
    @Html.CheckBoxFor(m => m.IsShortNews)
    @Html.LabelFor(m => m.IsShortNews, new { @class = "checkbox" })
</li>
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry to respond so late, I thought nobody gonna pay attention on friday. It's actually Code First so my database is autogenerated based on my entities. IS it enough just to declare IsShortNews as bool in my entity for EF to do all the job of creating the table with the correct types?
@Leron I guess you already found out since you accepted this answer, but yes you only need to declare the property as bool and EF will take care of the rest in a code first scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.