Похоже, вы хотите сделать id невидимым в URL-адресе, простой способ - изменить действие Удалить на тип сообщения.
Index.cs html:
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<form method="post" asp-action="Delete" asp-controller="Sprays">
<input type="hidden" value="@item.Id" name="id" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
}
</tbody>
</table>
Контроллер:
[HttpPost]
public ActionResult Delete(int? id)
{
//..
}
[HttpPost]
//[ActionName("Delete")]
public async Task<IActionResult> DeleteConfirm(int? id)
{
//...
}
Убедитесь, что ваш Delete.cs html должен быть таким, как показано ниже:
<form asp-action="DeleteConfirm" method="post" enctype="multipart/form-data">
Результат: Удалить Контроллер:
[HttpPost]
public async Task<IActionResult> Delete(string slug)
{
var data = HttpContext.Request.Form["id"].First();
var id = int.Parse(data);
//...
}
[HttpPost]
//[ActionName("Delete")]
public async Task<IActionResult> DeleteConfirm(int? id)
{
//...
}
Startup.cs:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{slug?}");
});
Результат:
введите описание изображения здесь
Весь контроллер:
public class TestsController : Controller
{
private readonly MyDbContext _context;
public TestsController(MyDbContext context)
{
_context = context;
}
// GET: Tests
public async Task<IActionResult> Index()
{
return View(await _context.Test.ToListAsync());
}
// GET: Tests/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var test = await _context.Test
.FirstOrDefaultAsync(m => m.Id == id);
if (test == null)
{
return NotFound();
}
return View(test);
}
// GET: Tests/Create
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name")] Test test)
{
if (ModelState.IsValid)
{
_context.Add(test);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(test);
}
// GET: Tests/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var test = await _context.Test.FindAsync(id);
if (test == null)
{
return NotFound();
}
return View(test);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name")] Test test)
{
if (id != test.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(test);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TestExists(test.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(test);
}
[HttpPost]
public async Task<IActionResult> Delete(string slug)
{
var data = HttpContext.Request.Form["id"].First();
var id = int.Parse(data);
var test = await _context.Test
.FirstOrDefaultAsync(m => m.Id == id);
if (test == null)
{
return NotFound();
}
return View(test);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var test = await _context.Test.FindAsync(id);
_context.Test.Remove(test);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool TestExists(int id)
{
return _context.Test.Any(e => e.Id == id);
}
}
Весь Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
}
// 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.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{slug?}");
});
}