Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建多个表/网格_C#_Asp.net Mvc_Asp.net Core Mvc - Fatal编程技术网

C# 创建多个表/网格

C# 创建多个表/网格,c#,asp.net-mvc,asp.net-core-mvc,C#,Asp.net Mvc,Asp.net Core Mvc,我刚刚开始学习基于此的ASP.NET核心MVC 我有一个数据库表“tblProducts”,我可以使用 型号: public class tblProducts { //SQL table is named tblProducts [Key] public int ID { get; set; } public DateTime Date { get; set; } public string Field1 { get; set; } publi

我刚刚开始学习基于此的ASP.NET核心MVC

我有一个数据库表“tblProducts”,我可以使用

型号

public class tblProducts
{
    //SQL table is named tblProducts

    [Key]
    public int ID { get; set; }
    public DateTime Date { get; set; }
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Product { get; set; } //<<Want separate tables split by this field
}
@model IEnumerable<LearnMVC.Models.tblProducts>

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Date)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Field1)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Field2)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Product)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Date)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Field1)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Field2)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Product)
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.ID">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }

    public DbSet<tblProducts> tblProducts { get; set; }
}
公共类TBL产品
{
//SQL表名为tblProducts
[关键]
公共int ID{get;set;}
公共日期时间日期{get;set;}
公共字符串字段1{get;set;}
公共字符串字段2{get;set;}
公共字符串产品{get;set;}/更新了,以更好地反映您不知道需要多少表的需求-很抱歉,第一次错过了这一点。与以前一样,小提琴不会运行,但它应该让您知道如何使用它

就我个人而言,我不喜欢在视图中包含这种逻辑,当然也可以将其移回控制器,但这更像是一种概念证明。

更新了,以更好地反映您关于不知道需要多少表的要求-抱歉,第一次错过了。与以前一样,小提琴可以esn不能跑,但它会让你知道该去哪里


就我个人而言,我不希望视图中有这种逻辑,当然也可以将其移回控制器,但这更像是一种概念证明。

查询数据库的代码是什么?在这里,您可以指定要“拆分”的产品查看部分视图和显示模板的数据。考虑创建一个视图模型,以一种对您有意义的方式来转换您的数据进行显示。也许通过产品将数据模型实体分组。然后您的视图可以简单地在视图模型集合中循环,以显示每一个表。查询的代码是什么?数据库?在那里您可以指定要“拆分”的产品查看部分视图和显示模板的数据。考虑创建一个视图模型,以一种对您有意义的方式来转换您的数据进行显示。也许通过产品对数据模型实体进行分组。然后,您的视图可以简单地在视图模型集合中循环,以显示每一个表。谢谢您的帮助。WOW如何?我能处理不知道有多少个产品的问题吗?在您的示例中,
class IndexViewModel
假设有3个产品是硬编码的,但它可以是任何数字,这取决于从db表返回的内容。很抱歉,第一次错过了这个要求-更新了我的答案-希望它能有所帮助。这非常好,非常有效,谢谢非常感谢。我所做的唯一更改是,视图中有3个foreach循环,而只需要2个感谢您的帮助。如果不知道有多少个产品,我将如何处理?示例中的
类IndexViewModel
假设有3个产品是硬编码的,但根据从db表。很抱歉,第一次错过了这个要求-更新了我的答案-希望有帮助。这非常好,非常感谢。我所做的唯一更改是视图中有3个foreach循环,而只需要2个
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }

    public DbSet<tblProducts> tblProducts { get; set; }
}