Praveen Kumar

Praveen Kumar

  • NA
  • 235
  • 22k

Getting Error while hitting Delete Button?

May 7 2020 12:35 AM
Controller
// GET: Admin/UserDefinedFields/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
using (var dbContextScope = _dbContextScopeFactory.CreateReadOnly())
{
UserCode userCode = attrRepo.FindBy<UserCode>(uc => uc.Id == id).FirstOrDefault();
if (userCode == null)
{
return HttpNotFound();
}
bool isUsed = false;
foreach (UserCodeValidValue ucvv in userCode.UserCodeValidValues)
{
if (ucvv.UserCodeValues.Count > 0)
{
isUsed = true;
break;
}
}
UserDefinedFieldsViewModel model = userCode;
model.IsUsed = isUsed;
return PartialView(model);
}
}
// POST: Admin/UserDefinedFields/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(UserDefinedFieldsViewModel model)
{
using (var dbContextScope = _dbContextScopeFactory.Create())
{
try
{
attrRepo.DeleteUserDefinedField(model.Id, _Login.GetCurrentUser());
dbContextScope.SaveChanges();
}
catch (Exception e)
{
logger.ErrorFormat("Exception deleting attribute", e);
ModelState.AddModelError("", "Error deleting attribute");
return PartialView(model);
}
}
return RedirectToAction("Index");
}
-------------------------------------------------------------------------------------------------------------------------
@model Redi2.RMIM.Web.Areas.Admin.Models.UserDefinedFieldsViewModel
@using (Html.BeginForm("Delete", "UserDefinedFields", FormMethod.Post))
{
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Delete Attribute</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
</div>
<div class="modal-body">
@if (Model.IsUsed)
{
<h4 class="text-danger"><i class="fa fa-exclamation"></i> This attribute is in use, so you cannot delete it.</h4>
}
else
{
<h4>Are you sure you want to delete this attribute?</h4>
}
<div class="form-horizontal">
<hr />
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.IsUsed)
<dl class="row">
<dt class="col-5 control-label">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class="col-6">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class="col-5 control-label">
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd class="col-6">
@Html.DisplayFor(model => model.Description) &nbsp;
</dd>
<dt class="col-5 control-label">
@Html.DisplayNameFor(model => model.UserCodeType)
</dt>
<dd class="col-6">
@Html.DisplayFor(model => model.UserCodeType)
</dd>
</dl>
</div>
</div>
<div class="modal-footer">
@if (!Model.IsUsed)
{
<input type="submit" value="Delete" id="submitButton" class="btn btn-primary" />
}
<button type="button" class="btn btn-outline" data-dismiss="modal">Cancel</button>
</div>
}
 
Error
 
 

Answers (7)