Home > Enterprise >  How to change a variable (MVC)
How to change a variable (MVC)

Time:09-13

I want to change values in the table from True, false To>>> Yes, No ,honesty I'm confused

enter image description here

enter image description here

CodePudding user response:

You can use a select list for this.

Edit like this your model:

public class ExampleViewModel : BaseCEViewModel
{
    public ExampleViewModel()
    {
        BoolList = new List<SelectListItem>();
        BoolList.Add(new SelectListItem
        {
            Text = "Yes",
            Value = "true"
        });
        BoolList.Add(new SelectListItem
        {
            Text = "No",
            Value = "false"
        });
        SelectedBools = new List<bool>();
    }

    public List<SelectListItem> BoolList { get; set; }
    public List<bool> SelectedBools { get; set; }
}

Now you can use a dropdownlist in your page, if you give same name to your dropdowns, values will add the SelectedBools Variable.

@Html.DropDownList("SelectedBools", Model.BoolList, new { @class = "form-control select2", placeholder = "...Select..." })

CodePudding user response:

As we clarify that you want text not a dropdown in your view

Don't use Html.DisplayFor(x => x.Active)

Try using if

Note: As you specify the variable to accepts null value. First check for null then use if statement

View

foreach(var item in Model)
{
    <tr>
        <td>
            if(item.Active != null)
            {
                if(Convert.ToBoolean(item.Active))
                {
                    <p>Yes</p>
                }
                else
                {
                    <p>No</p>
                }
            }
        </td>
        <td> ... </td>
    </tr>
}
  • Related