ASP.Net Core 7 实体关系和数据查看

huangapple go评论62阅读模式
英文:

ASP.Net Core 7 Entity Relationships and Viewing Data

问题

我正在使用ASP.NET Core 7,并且我有ProjectDataListItem两个模型。

我需要在这两者之间创建关系,以便我可以在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

根据你的问题,你似乎想要在ProjectDataListItem之间创建一个关系,然后在视图中显示与当前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 = &quot;Project Status&quot;)]
        [Required]
        public int Project_StatusId { get; set; }

        [Display(Name = &quot;Project Status&quot;)]
        [ForeignKey(&quot;Project_StatusId&quot;)]
        public DataListItem Project_Status { get; set; }

        public string propertyA { get; set; }
    }

public class DataListItem
    {
        [Key]
        public int DataListId { get; set; }

        [Display(Name = &quot;Value&quot;)]
        public string DataListValue { get; set; } = string.Empty;

        public ICollection&lt;Project&gt; Projects { get; set; }
    }

Now the table construct in database will be like:

ASP.Net Core 7 实体关系和数据查看

ASP.Net Core 7 实体关系和数据查看

If I want to show DataListValue in current view, I can use:

public IActionResult Privacy()
        {
            var value = _context.projects.Where(x =&gt; x.ProjectId == 1).Include(x =&gt; x.Project_Status).FirstOrDefault();
            return View(value);
        }

View

@model Project

@Html.DisplayFor(model =&gt; Model.Project_Status.DataListValue)

@Html.DisplayFor(model =&gt; Model.propertyA)

ASP.Net Core 7 实体关系和数据查看

Hope this demo can help you solve your issue.

huangapple
  • 本文由 发表于 2023年5月28日 09:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定