C# 在asp.net core 3.1 mvc中,如何在视图中显示类别名称而不是categoryId?
我有两个不同的表格,一个是标题,另一个是类别。 标题有4列,分别为名称、OWS、TWS和类别ID 类别有3列,分别为Id、Name、ParentName 我想在标题表上显示类别名称,而不是CategoryId 我使用外键搜索并看到一些解决方案。CategoryId未定义为FK。但是由于身份验证问题,我无法从db更改表 首先,我在标题模型中声明了虚拟类别C# 在asp.net core 3.1 mvc中,如何在视图中显示类别名称而不是categoryId?,c#,asp.net,entity-framework,model-view-controller,C#,Asp.net,Entity Framework,Model View Controller,我有两个不同的表格,一个是标题,另一个是类别。 标题有4列,分别为名称、OWS、TWS和类别ID 类别有3列,分别为Id、Name、ParentName 我想在标题表上显示类别名称,而不是CategoryId 我使用外键搜索并看到一些解决方案。CategoryId未定义为FK。但是由于身份验证问题,我无法从db更改表 首先,我在标题模型中声明了虚拟类别 public class Titles { public Guid Id { get; set; } public string
public class Titles
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OWS { get; set; }
public string TWS { get; set; }
public Guid CategoryId { get; set; }
public virtual Categories Category { get; set; }
}
然后我使用TitlesController Index()方法。当我写CategoryId=m.CategoryId时,我会在Titles表上看到CategoryId。我认为如果我使用Category.Name而不是CategoryId,我可以在Titles表中看到CategoryName。在这种情况下,我得到转换类型错误为字符串到系统guid
public async Task<IActionResult> Index()
{
var titles = _context.Titles
.Select(m => new Titles()
{
//Id=m.Id,
Name = m.Name,
OWS = m.OWS,
TWS = m.TWS,
CategoryId=m.Category.Name
});
return View(await titles.ToListAsync());
}
公共异步任务索引()
{
var titles=\u context.titles
.选择(m=>新标题()
{
//Id=m.Id,
Name=m.Name,
OWS=m.OWS,
TWS=m.TWS,
CategoryId=m.Category.Name
});
返回视图(等待titles.ToListAsync());
}
我认为,将CategoryName转换为Guid是没有意义的,而且将CategoryId转换为string可能会破坏项目的其他部分
因此,如何在不使用FK的情况下显示类别名称而不是categoryId?您可以在模型中添加一个属性,如下所示
public class Titles
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OWS { get; set; }
public string TWS { get; set; }
public Guid CategoryId { get; set; }
[NotMapped]
public string CategoryName { get; set; }
public virtual Categories Category { get; set; }
}
对于逻辑,如下所示
var titles = _context.Titles.Include(t=>t.Category)
.Select(m => new Titles()
{
//Id=m.Id,
Name = m.Name,
OWS = m.OWS,
TWS = m.TWS,
CategoryName=m.Category.Name
});
此外,您还可以创建单独的Dto而不是“未映射”属性,并将其直接用作视图的模型您可以在模型中添加一个“未映射”属性,如下所示
public class Titles
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OWS { get; set; }
public string TWS { get; set; }
public Guid CategoryId { get; set; }
[NotMapped]
public string CategoryName { get; set; }
public virtual Categories Category { get; set; }
}
对于逻辑,如下所示
var titles = _context.Titles.Include(t=>t.Category)
.Select(m => new Titles()
{
//Id=m.Id,
Name = m.Name,
OWS = m.OWS,
TWS = m.TWS,
CategoryName=m.Category.Name
});
此外,您还可以创建单独的Dto而不是“未映射”属性,并将其直接用作视图的模型您必须使用“未映射”属性
public class Titles
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OWS { get; set; }
public string TWS { get; set; }
public Guid CategoryId { get; set; }
[NotMapped]
public string CategoryName
public virtual Categories Category { get; set; }
}
及
必须使用NotMapped属性
public class Titles
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OWS { get; set; }
public string TWS { get; set; }
public Guid CategoryId { get; set; }
[NotMapped]
public string CategoryName
public virtual Categories Category { get; set; }
}
及