[英]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>
}
Try to use foreign key reference.尝试使用外键引用。
For reference: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application供参考: http : //www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp -net-mvc-应用程序
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.