创建多个表/网格

时间:2017-03-15 17:40:07

标签: c# asp.net-mvc asp.net-core-mvc

我刚刚开始基于此first-mvc-app tutorial

学习ASP.NET Core 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>

控制器:包含视图的默认MVC控制器,使用实体框架

public class tblProductsController : Controller
{
    private readonly ApplicationDbContext _context;

    public tblProductsController(ApplicationDbContext context)
    {
        _context = context;    
    }

    // GET: tblProducts
    public async Task<IActionResult> Index()
    {
        return View(await _context.tblProducts.ToListAsync());
    }

    // GET: tblProducts/Details/5
    public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var tblProducts = await _context.tblProducts.SingleOrDefaultAsync(m => m.ID == id);
        if (tblProducts == null)
        {
            return NotFound();
        }

        return View(tblProducts);
    }

    // GET: tblProducts/Create
    public IActionResult Create()
    {
        return View();
    }

    // POST: tblProducts/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("ID,Date,Field1,Field2,Product")] tblProducts tblProducts)
    {
        if (ModelState.IsValid)
        {
            _context.Add(tblProducts);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        return View(tblProducts);
    }

    // GET: tblProducts/Edit/5
    public async Task<IActionResult> Edit(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var tblProducts = await _context.tblProducts.SingleOrDefaultAsync(m => m.ID == id);
        if (tblProducts == null)
        {
            return NotFound();
        }
        return View(tblProducts);
    }

    // POST: tblProducts/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("ID,Date,Field1,Field2,Product")] tblProducts tblProducts)
    {
        if (id != tblProducts.ID)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(tblProducts);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!tblProductsExists(tblProducts.ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction("Index");
        }
        return View(tblProducts);
    }

    // GET: tblProducts/Delete/5
    public async Task<IActionResult> Delete(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var tblProducts = await _context.tblProducts.SingleOrDefaultAsync(m => m.ID == id);
        if (tblProducts == null)
        {
            return NotFound();
        }

        return View(tblProducts);
    }

    // POST: tblProducts/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> DeleteConfirmed(int id)
    {
        var tblProducts = await _context.tblProducts.SingleOrDefaultAsync(m => m.ID == id);
        _context.tblProducts.Remove(tblProducts);
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");
    }

    private bool tblProductsExists(int id)
    {
        return _context.tblProducts.Any(e => e.ID == id);
    }
}

数据\ ApplicationDbContext.cs

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; }
}

请有人帮忙解释(或了解任何好的教程)如何根据相同的模型字段创建多个表格,但每个表格都按照“产品&#39;产品”分类?

假设有5个产品,我希望看到5个表都具有相同的字段Date,Field1,Field2&amp;产品,但产品对每个表都是唯一的。

我不知道有多少产品,所以需要一些逻辑来确定需要多少个表。我不熟悉这一点,我不确定我是否需要多个模型,或者是否可以在控制器或视图中做一些聪明的事情,因为数据都来自同一个表格。

我尝试了一些搜索,但它们似乎是非mvc或基于多个不同的模型(例如,这类似但每个表{(3}}有不同的数据源)

TL; DR :ASP.NET MVC在单个数据库表中按字段显示多个表的方式是什么?

1 个答案:

答案 0 :(得分:0)

更新了fiddle以更好地反映您对不知道您需要多少桌子的要求 - 抱歉,第一次错过了。和以前一样,小提琴并没有运行,但它应该让你很好地了解它的用途。

就个人而言,我不希望在视图中不具备那种逻辑,并且将其移回控制器当然是可能的,但是将其视为更多的概念证明。