[英]Create multiple grids from one data source without duplicating that source
[英]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.