Hello
I am trying to establish Delete action using a string value. My table has customer_code that is a varchar field & I am unable to understand how to delete an existing record using the Controller Delete action.
My index (listing) cshtml as below
@model IEnumerable<CheckboxList_Net_Core5.Models.Registration>
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CustomerCode)
</th>
<th>
@Html.DisplayNameFor(model => model.CustomerName)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectedOrgs)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CustomerCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.SelectedOrgs)
<td>
@Html.ActionLink("Edit", "Edit", new { customercode = item.CustomerCode }) |
@Html.ActionLink("Details", "Details", new { customercode = item.CustomerCode }) |
@Html.ActionLink("Delete", "Delete", new { customercode = item.CustomerCode })
</td>
</tr>
}
</tbody>
</table>
<script type="text/javascript">
window.onload = function () {
alert( "The record that you tried to delete " + "@ViewBag.Message");
};
</script>
My delete action view cshtml
@model CheckboxList_Net_Core5.Models.Registration
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Registration</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.CustomerCode)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.CustomerCode)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.CustomerName)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.CustomerName)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.SelectedOrgs)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.SelectedOrgs)
</dd>
</dl>
@*<input type="hidden" name="CCode" value="5000" />*@
<form asp-action="Delete">
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-action="Index">Back to List</a>
</form>
</div>
and the Delete action controller code
// GET: OrgController/Delete/5
public ActionResult Delete(string customercode)
{
Registration registration = GetRegistrationData(customercode);
return View(registration);
}
// POST: OrgController/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(string customercode, Registration registration)
{
try
{
DeleteCustomer(customercode);
return RedirectToAction(nameof(Index));
}
catch
{
return View(registration);
}
}
It looks like HTTPpost is not receiving any data from the Delete.cshtml page when the form is submitted.
Please help