如何使用 ASP.NET Core 更新由依赖表组成的记录 [关闭]
Posted
技术标签:
【中文标题】如何使用 ASP.NET Core 更新由依赖表组成的记录 [关闭]【英文标题】:How to update record consisting of dependent tables using ASP.NET Core [closed] 【发布时间】:2021-10-16 17:03:57 【问题描述】:我可以列出和创建记录,但不能更新记录。当我运行我的项目并单击列出的记录上的编辑按钮时,会出现表单页面,但是如果我在完成表单后单击提交,我会在同一 URL 中找到页面未找到错误。
我可以列出和创建记录,但不能更新记录。当我运行我的项目并单击列出的记录上的编辑按钮时,会出现表单页面,但是如果我在完成表单后单击提交,我会在同一 URL 中找到页面未找到错误。
客户地址模型
namespace PharmacyManagmentV2.Models
public class CustomerAdressModel
public Customer customer get; set;
public Address address get; set;
客户控制器
namespace PharmacyManagmentV2.Areas.Application.Controllers
[Area("Application")]
public class CustomerController : Controller
private readonly AppDBContext _context;
private readonly IGenericRepository<Address> _address;
private readonly IGenericRepository<Customer> _customer;
public CustomerController(AppDBContext context,
IGenericRepository<Address> address,
IGenericRepository<Customer> customer)
_context = context;
_address = address;
_customer = customer;
// GET: Application/Customer
public async Task<IActionResult> Index()
var appDBContext = _customer.GetAll().Include(c => c.Address);
return View(await appDBContext.ToListAsync());
// GET: Application/Customer/Details/5
public async Task<IActionResult> Details(int? id)
if (id == null)
return NotFound();
var model = new CustomerAdressModel();
model.customer = await _customer.GetAll()
.Include(c => c.Address)
.FirstOrDefaultAsync(m => m.Id == id);
model.address = await _address.GetAll()
.Include(m => m.AspNetUsers)
.FirstOrDefaultAsync(m => m.Id == model.customer.AddressId);
if (model.customer == null)
return NotFound();
return View(model);
// GET: Application/Customer/Create
public IActionResult Create()
return View();
// POST: Application/Customer/Create
// To protect from overposting attacks, 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(CustomerAdressModel model)
if (ModelState.IsValid)
await _address.Create(model.address);
await _customer.Create(new Customer()
Address = model.address
);
await _address.SaveChanges();
await _customer.SaveChanges();
return RedirectToAction(nameof(Index));
return View(model);
// GET: Application/Customer/Edit/5
public async Task<IActionResult> Edit(int? id)
if (id == null)
return NotFound();
var model = new CustomerAdressModel();
model.customer = await _customer.GetById(id.Value);
model.address = await _address.GetById(model.customer.AddressId.Value);
if (model.customer == null)
return NotFound();
return View(model);
// POST: Application/Customer/Edit/5
// To protect from overposting attacks, 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, CustomerAdressModel model)
if (id != model.customer.Id)
return NotFound();
if (ModelState.IsValid)
try
await _customer.Update(model.customer);
await _customer.SaveChanges();
await _address.Update(model.address);
await _address.SaveChanges();
catch (DbUpdateConcurrencyException)
if (!CustomerExists(model.customer.Id))
return NotFound();
else
throw;
return RedirectToAction(nameof(Index));
return View(model);
// GET: Application/Customer/Delete/5
public async Task<IActionResult> Delete(int? id)
if (id == null)
return NotFound();
var customer = await _context.Customers
.Include(c => c.Address)
.FirstOrDefaultAsync(m => m.Id == id);
if (customer == null)
return NotFound();
return View(customer);
// POST: Application/Customer/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
var customer = await _context.Customers.FindAsync(id);
_context.Customers.Remove(customer);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
private bool CustomerExists(int id)
return _context.Customers.Any(e => e.Id == id);
Edit.cshtml
@model PharmacyManagmentV2.Models.CustomerAdressModel
@
ViewData["Title"] = "Edit";
Layout = "~/Areas/Application/Views/Shared/_Layout.cshtml";
<h1>Edit</h1>
<h4>CustomerViewModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit" >
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="@Model.customer.FirstName" class="control-label"></label>
<input asp-for="@Model.customer.FirstName" class="form-control" />
<span asp-validation-for="@Model.customer.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.customer.LastName" class="control-label"></label>
<input asp-for="@Model.customer.LastName" class="form-control" />
<span asp-validation-for="@Model.customer.LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.customer.Email" class="control-label"></label>
<input asp-for="@Model.customer.Email" class="form-control" />
<span asp-validation-for="@Model.customer.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.customer.Balance" class="control-label"></label>
<input asp-for="@Model.customer.Balance" class="form-control" />
<span asp-validation-for="@Model.customer.Balance" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.customer.Gender" class="control-label"></label>
<input asp-for="@Model.customer.Gender" class="form-control" />
<span asp-validation-for="@Model.customer.Gender" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.customer.DateofBird" class="control-label"></label>
<input asp-for="@Model.customer.DateofBird" class="form-control" />
<span asp-validation-for="@Model.customer.DateofBird" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.customer.Phone" class="control-label"></label>
<input asp-for="@Model.customer.Phone" class="form-control" />
<span asp-validation-for="@Model.customer.Phone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.address.City" class="control-label"></label>
<input asp-for="@Model.address.City" class="form-control" />
<span asp-validation-for="@Model.address.City" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.address.State" class="control-label"></label>
<input asp-for="@Model.address.State" class="form-control" />
<span asp-validation-for="@Model.address.State" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.address.Zip" class="control-label"></label>
<input asp-for="@Model.address.Zip" class="form-control" />
<span asp-validation-for="@Model.address.Zip" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.address.Address1" class="control-label"></label>
<input asp-for="@Model.address.Address1" class="form-control" />
<span asp-validation-for="@Model.address.Address1" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.address.Address2" class="control-label"></label>
<input asp-for="@Model.address.Address2" class="form-control" />
<span asp-validation-for="@Model.address.Address2" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts
@await Html.RenderPartialAsync("_ValidationScriptsPartial");
Startup.cs
namespace PharmacyManagmentV2
public class Startup
public Startup(IConfiguration configuration)
Configuration = configuration;
public IConfiguration Configuration get;
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
services.AddMvc();
services.AddControllersWithViews();
string connectionString = Configuration.GetConnectionString("default");
services.AddDbContext<AppDBContext>(c => c.UseSqlServer(connectionString));
services.AddIdentity<ApplicationUser, ApplicationRole>(opt =>
opt.Password.RequireDigit = false;
opt.Password.RequireLowercase = false;
opt.Password.RequiredLength = 1;
opt.Password.RequireUppercase = false;
opt.Password.RequireNonAlphanumeric = false;
).AddEntityFrameworkStores<AppDBContext>();
services.ConfigureApplicationCookie(opt =>
opt.Cookie.Name = "CookieSettings";
opt.LoginPath = new PathString("/Account/Login");
opt.Cookie.HttpOnly = true;
opt.Cookie.SameSite = SameSiteMode.Strict;
opt.ExpireTimeSpan = TimeSpan.FromMinutes(30);
);
services.PostConfigure<CookieAuthenticationOptions>(IdentityConstants.ApplicationScheme,
opt =>
opt.Cookie.Name = "CookieSettings";
opt.LoginPath = new PathString("/Account/Login");
opt.Cookie.HttpOnly = true;
opt.Cookie.SameSite = SameSiteMode.Strict;
opt.ExpireTimeSpan = TimeSpan.FromMinutes(30);
);
services.AddScoped<IAddressRepository, AddressRepository>();
services.AddScoped<ICategoryRepository, CategoryRepository>();
services.AddScoped<ICustomerRepository, CustomerRepository>();
services.AddScoped<ILeafRepository, LeafRepository>();
services.AddScoped<IManufacturerRepository, ManufacturerRepository>();
services.AddScoped<IMedicineRepository, MedicineRepository>();
services.AddScoped<IMedicineTypeRepository, MedicineTypeRepository>();
services.AddScoped<IPurchaseRepository, PurchaseRepository>();
services.AddScoped<ISellRepository, SellRepository>();
services.AddScoped<IUnitRepository, UnitRepository>();
services.AddScoped(typeof(IGenericRepository<>),typeof(GenericRepository <>));
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
endpoints.MapControllerRoute(
name: "default",
pattern: "area=Application/controller=Customer/action=Index/id?");
endpoints.MapControllerRoute(
name: "Login",
pattern: "controller=Account/action=Login/id?");
endpoints.MapControllerRoute(
name: "Register",
pattern: "controller=Account/action=Login/id?");
endpoints.MapControllerRoute(
name: "application_home",
pattern: "area=Application/controller=Home/action=Index/id?");
endpoints.MapControllerRoute(
name: "customer_list",
pattern: "area=Application/controller=Customer/action=Index/id?");
endpoints.MapControllerRoute(
name: "customer_edit",
pattern: "area=Application/controller=Customer/action=Edit/id?");
);
【问题讨论】:
你也必须展示你的控制器 它说:看起来你的帖子主要是代码;请添加更多详细信息。 抱歉,我看不到您的控制器标题,只有操作 我重写了文本以通过警告 【参考方案1】:由于您在操作中使用 [ValidateAntiForgeryToken] 尝试添加 @Html.AntiForgeryToken()
<form asp-action="Edit" >
@Html.AntiForgeryToken()
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
或删除 [ValidateAntiForgeryToken]
【讨论】:
以上是关于如何使用 ASP.NET Core 更新由依赖表组成的记录 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ASP.NET Core 中使用 Bootstrap 4
ASP.NET Core依赖注入高级玩法——如何注入多个服务实现类
ASP.NET Core Web 应用程序系列- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)