我通过脚手架为以下模型创建了一个“创建”剃刀页面:
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProdId { get; set; }
[Required]
public string ProdName { get; set; }
[Required]
public float Price { get; set; }
[Required]
[ForeignKey("Category")]
public int CategoryID { get; set; }
[Required]
[ForeignKey("Supplier")]
public int SupplierID { get; set; }
public virtual Category category { get; set; }
public virtual Supplier supplier { get; set; }
public List<Review> reviews { get; set; }
public List<ShoppingCart> shoppingCarts { get; set; }
public List<Order> orders { get; set; }
}
创建了以下剃刀页面:
@page
@model WebStore.Pages.Products.CreateModel
@using Microsoft.AspNetCore.Mvc.ViewEngines
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Create</h1>
<h4>Product</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Product.ProdName" class="control-label"></label>
<input asp-for="Product.ProdName" class="form-control" />
<span asp-validation-for="Product.ProdName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Product.Price" class="control-label"></label>
<input asp-for="Product.Price" class="form-control" />
<span asp-validation-for="Product.Price" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Product.CategoryID" class="control-label"></label>
<select asp-for="Product.CategoryID" class ="form-control" asp-items="ViewBag.CategoryID"></select>
</div>
<div class="form-group">
<label asp-for="Product.SupplierID" class="control-label"></label>
<select asp-for="Product.SupplierID" class ="form-control" asp-items="ViewBag.SupplierID"></select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
使用以下create.cs相关文件:
public class CreateModel : PageModel
{
private readonly WebStore.MyDbContext _context;
public CreateModel(WebStore.MyDbContext context)
{
_context = context;
}
public IActionResult OnGet()
{
ViewData["CategoryID"] = new SelectList(_context.Categories, "CatId", "CatDescription");
ViewData["SupplierID"] = new SelectList(_context.Suppliers, "supplierId", "supplierId");
return Page();
}
[BindProperty]
public Product Product { get; set; } = default!;
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid || _context.Products == null || Product == null)
{
return Page();
}
_context.Products.Add(Product);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
我面临的问题是,当我尝试添加另一个项目并单击“创建”时,实际上没有添加任何项目,数据库中没有任何更改,也没有重定向到索引。
CategoryID和supplierID引用其他实体。
可以调试页面吗?我相信您的 _context (您是否注册了 DbContext)或您的产品为空或 ModelState 无效。在这种情况下,页面将在将产品保存到数据库之前返回。