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(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))
{
Delete Attribute
@if (Model.IsUsed)
{
This attribute is in use, so you cannot delete it.
}
else
{
Are you sure you want to delete this attribute?
}
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.IsUsed)
@Html.DisplayNameFor(model => model.Name)
@Html.DisplayFor(model => model.Name)
@Html.DisplayNameFor(model => model.Description)
@Html.DisplayFor(model => model.Description)
@Html.DisplayNameFor(model => model.UserCodeType)
@Html.DisplayFor(model => model.UserCodeType)
@if (!Model.IsUsed)
{
}
}
Error
7 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Mayank GaurPosted May 7, 2020, 2:20 AM
Praveen KumarPosted May 7, 2020, 2:15 AM
Salman BegPosted May 7, 2020, 2:12 AM
Mayank GaurPosted May 7, 2020, 2:03 AM
Praveen KumarPosted May 7, 2020, 2:00 AM
Salman BegPosted May 7, 2020, 1:39 AM
Mayank GaurPosted May 7, 2020, 1:29 AM