DharaPPatel
DharaPPatel

Reputation: 12743

Replace an integer value with a string when displaying in a view in MVC

i have view like this :

@model IEnumerable<PMS.Models.Activity_Contractor>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutPageArchitect.cshtml";
  }

 <h2>Index</h2>

 <p>
   @*@Html.ActionLink("Create New", "Create")*@
  </p>
<table>
<tr>
     @*<th>
        @Html.DisplayNameFor(model => model.A_C_ID)
    </th>*@

    <th>
        @Html.DisplayName("ProjectName")
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ProjectActivityMaster.ActivityName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ContractorMaster.ContractorName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IsActive)
    </th>
  @*  <th>
        @Html.DisplayNameFor(model => model.CreatedBy)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.CreatedON)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.UpdatedBy)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.UpdatedON)
    </th>*@
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    @*<td>
        @Html.DisplayFor(modelItem => item.A_C_ID)
    </td>*@
    <td>
        @Html.Label("Project")
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ProjectActivityMaster.ActivityName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ContractorMaster.ContractorName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IsActive)
    </td>
   @* <td>
        @Html.DisplayFor(modelItem => item.CreatedBy)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CreatedON)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UpdatedBy)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UpdatedON)
    </td>*@
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.A_C_ID  }) |
        @Html.ActionLink("Details", "Details", new { id=item.A_C_ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.A_C_ID })
    </td>
     </tr>
}

 </table>

my item.IsActive value used to be 1/0 i want saw it as True if 1 and False id 0. how can i do that ?

Upvotes: 0

Views: 2312

Answers (2)

von v.
von v.

Reputation: 17108

my item.IsActive value used to be 1/0 i want saw it as True if 1 and False id 0. how can i do that ?

INT which contains 1/0

If you want total control over what you can show then it's best that you use a display template.

Create A Template

Create a folder called DisplayTemplates under the Shared folder, if you still don't have it. Then create a file called CustomInt.cshtml, you can of course choose any name you want except those names that are similar to data types (e.g. Int, Boolean, etc). Then on the template you need to have this:

@model System.Int32

@if (Model == 0)
{
    @:True
}
else if (Model==1) {
    @:False
}

How You Use It

Then on your view where you want to show IsActive you need to do this:

@Html.DisplayFor(m => m.IsActive, "CustomInt")

Notice that we specified a template name to html helper.

Pitfall

You need to be very careful in using the template. Since IsActive is an int then it can have a value other than 0 and 1. So if the value is other than those two then nothing will print out. But that's not a big issue though as you can easily add another else and print an error or something else. But I think you are pretty sure that IsActive will always have either 0 or 1.

The modified template to catch unexpected value:

@model System.Int32

@if (Model == 0)
{
    @:True
}
else if (Model==1) {
    @:False
}
else {
    @:What is this value? The system was hacked!
}

Upvotes: 1

Ian
Ian

Reputation: 67

You could parse your IsActive on your viewmodel to return true or false.

public class Myviewmodel
{
    public string IsActive { get; set; }

}

public class Mymodel
{
    public bool IsActive { get; set; }
}

then parse your booleans to strings when mapping them from your model to your viewmodel?

Tip : Try using NBuilder

Upvotes: 0

Related Questions