I can list and create records, but can't update the records. When I run my project and click the Edit button on listed records, form page appears, but if I click Submit after completing the form, I get page not found error in same URL.
I can list and create records, but can't update the records. When I run my project and click the Edit button on listed records, form page appears, but if I click Submit after completing the form, I get page not found error in same URL.

CustomerAddressModel
namespace PharmacyManagmentV2.Models
{
public class CustomerAdressModel
{
public Customer customer { get; set; }
public Address address { get; set; }
}
}
CustomerController
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?}");
});
}
}
}