I have been trying to figure out how to display yes / no instead of a bool result of true / false to make this easier to read for my users.
I am using c# and visual studio 2015 and I have taken a look and try to apply some of the techniques noted here in stackoverflow here and here.
Im not to sure which is the best way to resolve the query that I have. In a nutshell I have a database table that holds 3 columns with bit datatypes (eg. A, B & C). This table is the fed through the use of entity framework to my MVC project.
The model associated with this table holds these columns as bool. I an using this table to list items in the database, however the results for these items display True / False to the user (see image below).
I have tried to change my Razor code to complete this task but have failed.
@item.AttachmentA.isTrue?"Yes":"No")
And I have also tried this,
@Html.DisplayFor(modelItem => item.AttachmentA.isTrue?"Yes":"No")
For both above, I'm getting compilation error that states,
'System.Nullable' does not contain a definition for 'isTrue' and no extension method 'isTrue' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)
I cant quite seem to get the above working. Would I be able to complete an if statement in my controller to complete this?
I have tried to complete this with something like the following code but this doesn't seem to work.
// GET: View for ACTIVE items and set status of the bool for true / false
public ActionResult Active()
{
List<vwStatusActive> clientRecordsActive = db.vwStatusActives.ToList();
foreach (vwStatusActive active in clientRecordsActive)
{
if (vwStatusActive == true)
{
vwStatusActive = 'Yes';
}
else
{
vwStatusActive = 'No';
}
}
return View(clientRecordsActive);
}
I have changed the above if statement in the controller to now hold variables for the bool true / false.
// GET: View for ACTIVE items and set status of the bool for true / false
public ActionResult Active()
{
List<vwStatusActive> clientRecordsActive = db.vwStatusActives.ToList();
bool t = true;
bool f = false;
foreach (vwStatusActive active in clientRecordsActive)
{
if (t == true)
{
t = "Yes";
}
else
{
t = "No";
}
}
return View(clientRecordsActive);
}
I now however get two error for the string items "Yes", "No" that state,
Cannot implicitly convert type 'string' to 'bool
If anyone has any suggestions on best practices, can you please advise?