簡體   English   中英

創建多個表/網格

[英]Create multiple tables/grids

我剛開始根據此first-mvc-app教程學習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>

Contoller :使用實體框架的帶有視圖的默認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);
    }
}

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

請有人幫忙解釋一下(或知道任何好的教程)如何根據相同的模型字段創建多個表,但每個表都由“產品”列拆分嗎?

假設有5種產品,我希望看到5個表都具有相同的字段Date,Field1,Field2和Product,但是Product對於每個表都是唯一的。

我不會預先知道有多少種產品,因此需要一些邏輯來確定需要多少張表。 由於這是我的新手,因此我不確定是否需要多個模型,或者是否可以在控制器或視圖中完成某些巧妙的操作,因為數據全部來自同一張表。

我嘗試了一些搜索,但它們似乎不是非mvc或基於多個不同的模型(例如,這很相似,但每個表的數據源不同http://www.compilemode.com/2016/09/show-在asp-net-mvc.html中查看多個表數據

TL; DR :ASP.NET MVC顯示從單個數據庫表中按字段划分的多個表的方式是什么?

更新了小提琴,以更好地反映您對不知道需要多少張桌子的要求-抱歉,第一次錯過了。 和以前一樣,小提琴不會運行,但是應該可以讓您很好地了解如何使用它。

就個人而言,我不希望視圖中具有這種邏輯,當然可以將其移回控制器,但可以將其視為概念的證明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM