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.
byteinstead of aboolif your values are 1 or 0? Or am I missing something?