在MVC中的视图中显示时,用字符串替换整数值 [英] Replace an integer value with a string when displaying in a view in MVC

查看:77
本文介绍了在MVC中的视图中显示时,用字符串替换整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的观点:

 @ model IEnumerable <   PMS.Models.Activity_Contractor  >  

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

< h2 > 索引< / h2 >

< p >
@ * @ Html.ActionLink(新建,创建)* @
< / p >
< 表格 >
< 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.Cont ractorName)
< / 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 >
< / tr >

@foreach(模型中的var项目){
< tr >
@ * < < span class =code-leadattribute> 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(编辑,编辑,新{id = item.A_C_ID})|
@ Html.ActionLink(详细信息,详细信息,新{id = item.A_C_ID})|
@ Html.ActionLink(删除,删除,新{id = item.A_C_ID})
< / td >
< / tr >
}

< / table >





我的item.IsActive值曾经是1/0我希望将其视为True如果1和False id为0.我怎么能这样做?



如果我想拥有其他字段的名称,例如statusofproduct,其中有0,1,2我希望显示挂起,延迟,收到它

解决方案

从视图中你可以检查isActive td中的条件,如下所示。请删除 @ Html.DisplayFor(modelItem => item.IsActive)并将下面的代码放在那里

< pre lang =c#>< table>< tbody>< tr>< td>
if (item.IsActive)
{
Html.Display( True
}
else
{
Html.Display( False
}
< / td > < / tr > < / tbody > < / 表格 >



希望这有助于


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 see it as True if 1 and False id 0. how can i do that ?

what if i want to have name for some other field like statusofproduct which has 0,1,2 and i want display pending,delayed,received for it

解决方案

From the View you can check condition inside the isActive td like below.Please remove @Html.DisplayFor(modelItem => item.IsActive) and put the below code there

<table><tbody><tr><td>
if(item.IsActive)
{
Html.Display("True")
}
else
{
Html.Display("False")
}
</td></tr></tbody></table>


Hope this helps


这篇关于在MVC中的视图中显示时,用字符串替换整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆