MVC model boolean display yes or no

2019-03-11 19:43发布

问题:

i have a boolean field in my model in mvc 4 entity framework 4.5

i want to display the field in my view

i use this call

@item.isTrue

but i got true or false,

i want to get yes when true and no when false

what should i do please?

回答1:

In your view:

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


回答2:

You could use a custom html helper extension method like this:

@Html.YesNo(item.IsTrue)

Here is the code for this:

public static MvcHtmlString YesNo(this HtmlHelper htmlHelper, bool yesNo)
{
    var text = yesNo ? "Yes" : "No";
    return new MvcHtmlString(text);
}

This way you could re-use it throughout the site with a single line of Razor code.



回答3:

To expand on DigitalD's answer, you could consider wrapping this up in an extension method:

public static string ToFriendlyString(this Boolean b)
{
    return b ? "Yes" : "No";
}

Then you can use it all over the place:

@item.IsTrue.ToFriendlyString()


回答4:

This is a little late, but...

One useful method missed by other answers answers is the Custom DisplayTemplate method. By putting this Code:

@model bool
<p>@{Model ? "Yes" : "No"}</p>

Into a Partial View (Maybe YesNo.cshtml) in the Display Templates folder (/Views/Shared/DisplayTemplates). Then, in your view, use this line:

@Html.Display(item.isTrue,"YesNo")

where "YesNo" is whatever you named your Partial View, minus the .cshtml extension.

By adding the second string (the templateName), you tell DisplayExtensions to display the boolean with your custom template, rather than the default method (a checkbox).

This method may not be the simplest for this situation, but it comes in handy with more complex situations (such as, say, a custom calendar for selecting dates)



回答5:

In your model write something like this:

public Nullable<bool> Active
{
    get;
    set;
}
public string ISActive
{
    get
    {
        return (bool)this.Active ? "Yes" : "NO";
    }
}

Active is a boolean property, we create ISActive property for reading its value and show appropriate message to user.



回答6:

If you're in a rush, or you use an integer instead of bit. (I do this in case you have a third option later and your yes/no becomes yes, no, octopus,as mine always seem to do, here is the simple plain quick dirty way of going... declare your variable at the top of the Razor view:

@model MyModel
@using My.Models;
@{
    ViewBag.Title = "Index";
    ViewBag.ReturnUrl = "";
    string Active = ""; //This will hold your "Yes" or "No"
 }

As you go through your items...The following code will set your string variable Active to "Yes" or "No" and then you simply display it as is on the page (with no @Html.DisplayFor(...) markup.

@{if (item.Active == 0){Active = "No";}else{Active = "Yes";}}
@Active

In case you don't know already, surrounding the entire "if" Statement with @{} makes life easy - no @'s are needed within the brackets...



回答7:

There is a solution similar to @Lucas Niewohner's, taken from Scott on Writing .NET.

Create a DisplayTemplate called YesNo.cshtml:

@model bool
@if (Model)
{
    @Html.Raw("Yes")
}
else
{
    @Html.Raw("No")
}

Then decorate the ViewModel with UIHint to wire it up:

public class foo
{
...
[UIHint="YesNo"]
public bool SomeBooleanValue { get; set; }
...
}

Now you can use your MVC helpers as usual:

...
@Html.DisplayFor (x=> x.SomeBooleanValue)
...

You can take it one step further to create an EditorTemplate YesNo.cshtml as well:

@model bool
@Html.DropDownList("", new SelectListItem[] { new SelectListItem() { Text = "Yes", Value = "true", Selected = Model }, new SelectListItem() { Text = "No", Value = "false", Selected = !Model }})

This approach is particularly helpful if you want to use the ViewModel in several places with a consistent implementation since you only have to associate the template in one place.