简体   繁体   English

ASP.NET MVC。 如何在视图页面中显示测试而不是 id?

[英]ASP.NET MVC . How to display test instead of id in view page?

I have a view (Index) which display data from a model (TBL_PROJECT).我有一个视图(索引),它显示来自模型(TBL_PROJECT)的数据。

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(m => item.PROJ_YEAR)
        </td>
        <td>
            @Html.DisplayFor(m => item.PROJ_NAME)
        </td>
        <td>
            @Html.DisplayFor(m => item.PROJ_STATUS)
        </td>
    </tr>
}

When displayed it would be:显示时将是:
1998 Paint 1 1998 油漆 1
1998 Cleaning 1 1998年清洁1

I need the status to be in text, which is in another model(TBL_STATUS).我需要状态在文本中,它在另一个模型中(TBL_STATUS)。
What should I do?我该怎么办?
This is the TBL_STATUS Model这是 TBL_STATUS 模型

public class TBL_STATUS
{
    public int ID { get; set; }
    public string STATUS { get; set; }
}



This is the controller这是控制器

public ActionResult Index(int page = 1, string Query = null)
{
    var projectList = db.TBL_PROJECTS.Include(x => x.TBL_STATUS).Where(x => x.PROJ_CAT == 1).ToList().OrderByDescending(x => x.ID);

    if (!string.IsNullOrEmpty(Query))
    {
        projectList = projectList
            .Where(x => x.PROJ_NAME.ToLower().Contains(Query.ToLower()))
            .ToList().OrderByDescending(x => x.ID);
    }
    return View(new PagedList.PagedList<TBL_PROJECT>(projectList, page, 10));
}

Update: I found out a way the way to do it.更新:我找到了一种方法来做到这一点。 In the model (TBL_PROJECT) I add this:在模型 (TBL_PROJECT) 中,我添加了以下内容:

    [Display(Name = "Current Status")]
    public int PROJ_STATUS { get; set; }

    [ForeignKey("PROJ_STATUS")]
    public TBL_STATUS TBL_STATUS { get; set; }


and when calling it in the View:...并在视图中调用它时:...

 <td>
    @Html.DisplayFor(m => item.TBL_STATUS.STATUS)
 </td>


Now I have another issue.现在我有另一个问题。 Same issue, I want to add a district (which is an int in the TBL_PROJECT which refer to another model (TBL_DISTRICT)).同样的问题,我想添加一个区(它是 TBL_PROJECT 中的一个 int,它指的是另一个模型(TBL_DISTRICT))。

    public class TBL_DISTRICT
{
    public int ID { get; set; }
    public string DIST_NAME { get; set; }
    public string DIST_OFFICE { get; set; }
    public string DIST_CODE { get; set; }
}

Now, in my TBL_PROJECT model, I add these:现在,在我的 TBL_PROJECT 模型中,我添加了这些:

    [Display(Name = "District")]
    public int DISTRICT { get; set; }

    [ForeignKey("DISTRICT")]
    public TBL_DISTRICT TBL_DISTRICT { get; set; }


And in the view:在视图中:

@Html.DisplayFor(m => item.TBL_DISTRICT.DIST_NAME ) @Html.DisplayFor(m => item.TBL_DISTRICT.DIST_NAME)

Theres no error but it returned nothing.没有错误,但它什么也没返回。 Do I need to add constraint between these tables (TBL_PROJECT & TBL_DISTRICT)?我是否需要在这些表(TBL_PROJECT 和 TBL_DISTRICT)之间添加约束?

Add the following line in TBL_PROJECT Model.在 TBL_PROJECT 模型中添加以下行。

public class TBL_PROJECT
{
//Code you have
public virtual TBL_STATUS TBL { get; set; }
}

Then modify the view然后修改视图

@foreach (var item in Model)
        {
                <tr>
                    <td>
                        @Html.DisplayFor(m => item.PROJ_YEAR.STATUS)
                    </td>
                    <td>
                        @Html.DisplayFor(m => item.PROJ_NAME)
                    </td>
                    <td>
                        @Html.DisplayFor(m => item.TBL.STATUS)
                    </td>
                </tr>
            }

Found the answer.找到了答案。 I put this first line in the controller.我把第一行放在控制器中。

var projectList = db.TBL_PROJECTS.Include(x => x.TBL_STATUS).Include(x => x.TBL_DISTRICT).Where(x => x.PROJ_CAT == 1).ToList().OrderByDescending(x => x.ID);

I just add another Include(x => x.TableName)我只是添加另一个 Include(x => x.TableName)

使用@Html.Display(item.PROJ_STATUS.ToString())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ASP.NET MVC2无法显示视图页面 - Unable to display view page using ASP.NET MVC2 如何让 ASP.NET MVC Ajax 响应重定向到新页面而不是将视图插入到 UpdateTargetId 中? - How to get an ASP.NET MVC Ajax response to redirect to new page instead of inserting view into UpdateTargetId? 如何在 ASP.NET MVC 的录取表格成功页面上显示来自 model 的 id? - How to display id from model on a admission form success page in ASP.NET MVC? 如何从控制器 ASP.NET MVC 的视图页面中自动添加所选 id 的名称 - How to automatically add a name from selected id in View page from controller ASP.NET MVC 如何在 ASP.NET Core 中显示名称而不是 id? - How to display name instead of id in ASP.NET Core? 如何在asp.net mvc视图页面中显示人类可读的xml - how to display human readable xml in an asp.net mvc view page 如何在ASP.NET MVC中显示具有相同ID的所有行? - How to display all rows with same ID in ASP.NET MVC? 如何测试 ASP.NET MVC controller 并查看 - How to test an ASP.NET MVC controller and view ASP.Net MVC WebAPI - 如何在视图中显示 json 数据 - ASP.Net MVC WebAPI - How to display json data in View 如何在ASP.NET MVC 4 Razor项目的视图中显示集合? - How to display a collection in View of ASP.NET MVC 4 Razor project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM