英文:
ASP.Net Core 7 Entity Relationships and Viewing Data
问题
我正在使用ASP.NET Core 7,并且我有Project
和DataListItem
两个模型。
我需要在这两者之间创建关系,以便我可以在DataListItem
的视图中显示值(DataListValue
)。
Project
类可能会有多个对DataListItem
模型的引用。
public class Project
{
ProjectId
[Display(Name = "Project Status")]
[Required]
[ForeignKey("DataListItem")]
public int Project_Status { get; set; }
}
我有一个名为DataListItem
的模型
public class DataListItem
{
[Key]
public int DataListId { get; set; }
[Display(Name = "Value")]
public string DataListValue { get; set; } = string.Empty;
[Display(Name = "Default")]
}
我想创建这两者之间的关系,以便在我的视图中,我可以添加类似以下内容:
@Html.DisplayFor(model => project.Project_Status.DataListValue)
我已经尝试了多种方法,但一直在努力。
英文:
I'm using ASP.NET Core 7 and I have models Project
and DataListItem
.
I need to create a relationship between the two so that I can display the value in the view of the DataListItem
(DataListValue
).
The Project
class may have multiple references to the DataListItem
model.
public class Project
{
ProjectId
[Display(Name = "Project Status")]
[Required]
[ForeignKey("DataListItem")]
public int Project_Status { get; set; }
}
I have a model called DataListItem
public class DataListItem
{
[Key]
public int DataListId { get; set; }
[Display(Name = "Value")]
public string DataListValue { get; set; } = string.Empty;
[Display(Name = "Default")]
}
I want to create a relationship between the two such that in my view I can add something like this
@Html.DisplayFor(model => project.Project_Status.DataListValue)
Have tried multiple ways and I am struggling.
答案1
得分: 0
根据你的问题,你似乎想要在Project
和DataListItem
之间创建一个关系,然后在视图中显示与当前Project
对应的DataListItem
属性。由于我不知道你想要创建什么样的关系,所以在这里我创建了一个示例的“一对多”关系。
public class Project
{
public int ProjectId { get; set; }
[Display(Name = "Project Status")]
[Required]
public int Project_StatusId { get; set; }
[Display(Name = "Project Status")]
[ForeignKey("Project_StatusId")]
public DataListItem Project_Status { get; set; }
public string propertyA { get; set; }
}
public class DataListItem
{
[Key]
public int DataListId { get; set; }
[Display(Name = "Value")]
public string DataListValue { get; set; } = string.Empty;
public ICollection<Project> Projects { get; set; }
}
现在数据库中的表结构将如下所示:
如果我想在当前视图中显示DataListValue
,我可以使用以下方法:
public IActionResult Privacy()
{
var value = _context.projects.Where(x => x.ProjectId == 1).Include(x => x.Project_Status).FirstOrDefault();
return View(value);
}
视图:
@model Project
@Html.DisplayFor(model => Model.Project_Status.DataListValue)
@Html.DisplayFor(model => Model.propertyA)
希望这个示例可以帮助你解决你的问题。
英文:
From your question, you seems to wanna create a relationship between Project
and DataListItem
, Then in the view show the property in DataListItem
which correspond to the current Project
. Because I don't know what kind of relationship you want to create, So here i create a one-to-many
relationship as example.
public class Project
{
public int ProjectId { get; set; }
[Display(Name = "Project Status")]
[Required]
public int Project_StatusId { get; set; }
[Display(Name = "Project Status")]
[ForeignKey("Project_StatusId")]
public DataListItem Project_Status { get; set; }
public string propertyA { get; set; }
}
public class DataListItem
{
[Key]
public int DataListId { get; set; }
[Display(Name = "Value")]
public string DataListValue { get; set; } = string.Empty;
public ICollection<Project> Projects { get; set; }
}
Now the table construct in database will be like:
If I want to show DataListValue
in current view, I can use:
public IActionResult Privacy()
{
var value = _context.projects.Where(x => x.ProjectId == 1).Include(x => x.Project_Status).FirstOrDefault();
return View(value);
}
View
@model Project
@Html.DisplayFor(model => Model.Project_Status.DataListValue)
@Html.DisplayFor(model => Model.propertyA)
Hope this demo can help you solve your issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论