0

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).

Screen Shot of results

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?

user276648
  • 6,018
  • 6
  • 60
  • 86
Betty Lazzari
  • 99
  • 1
  • 6
  • 13
  • 1
    Hi! Please can you edit your question replacing 'doesn't work' with actual compiler errors / runtime errors. Thanks! – Baldrick Jul 04 '17 at 11:18
  • You are trying to assign values to a type. Anyway a bit more code and errors would help to focus on the issue. Where are the variables that store the rendered string? – Steak Overflow Jul 04 '17 at 11:20
  • See example here for changing the dropwdown's display value: https://stackoverflow.com/a/9517695/1489570 – MalvEarp Jul 04 '17 at 11:24
  • 2
    You missed start bracket and @ in yur code. Do like this and let me know. **@(item.AttachmentA.isTrue?"Yes":"No")** – Basanta Matia Jul 04 '17 at 11:26
  • 1
    What is `vwStatusActive`? You are using it as a Type (`List`), then test it against a `bool` and then try to assign a `string` value. – Hans Kesting Jul 04 '17 at 11:41
  • In your Active Action method, you need to write like this, `if (vwStatusActive == true) { active.vwStatusActive = 'Yes'; }` You missed **active.** – Basanta Matia Jul 04 '17 at 11:43
  • The message about `item.AttachmentA.isTrue` says that (the Type of) "AttachmentA" (which apparently is some Nullable type) doesn't have an "isTrue" property. You might need `item.AttachmentA.Value.isTrue` (but this will fail if AttachmentA is null). – Hans Kesting Jul 04 '17 at 12:21
  • @BasantaMatia many thanks, I have made the change for the empty bracket but this doesn't resolve the problem. I get the following error message 'bool' does not contain a definition for 'isTrue' and no extension method 'isTrue' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?) – Betty Lazzari Jul 04 '17 at 13:26
  • @HansKesting many thanks, I have added the additional .Value attribute but this doesn't resolve the problem. I get the following error message 'bool' does not contain a definition for 'isTrue' and no extension method 'isTrue' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?) – Betty Lazzari Jul 04 '17 at 13:27
  • @HansKesting vwStatusActive is a view model that lists all my properties for that item. The data is pulled in fine by using the code line: List clientRecordsActive = db.vwStatusActives.ToList(); However I thought that if I can note complete this task using Razor, I could maybe pull the list of items back in the controller and then use a foreach loop to loop through the records and display any true status to yes and any false to no. – Betty Lazzari Jul 04 '17 at 13:29

1 Answers1

2

If AttachmentA property is of type bool then use

@(item.AttachmentA ? "Yes":"No")

OR

@{var isAttachmentA = (item.AttachmentA ? "Yes" : "No");} @Html.DisplayFor(modelItem => isAttachmentA )

If AttachmentA property is of type bool? (Nullable bool) then use

@(item.AttachmentA.GetValueOrDefault() ? "Yes":"No")

OR

@{var isAttachmentA = (item.AttachmentA.GetValueOrDefault() ? "Yes" : "No");} @Html.DisplayFor(modelItem => isAttachmentA )

Pawan Rai
  • 21
  • 2