This is my table
Userid UserType
1 Admin
2 Members
3 Staff
4 DataEntry
5 Auditors
When i insert user if it is already exist then the error thrown
when i update user "IF I CLICK SAVE CHANGES" that will navigate list as it is.
if i modified that value dose not allow if it is already exist.
upto insertion my code is working but when i use update that error will display
so please help me..
Here is my code
-----Model
public class AdminModel
{
public int PUserTypeID { get; set; }
[Required(ErrorMessage = " Please enter User Type.")]
public string PDescription { get; set; }
public string PResult { get; set; }
public string Status { get; set; }
}
public class AdminList
{
public List<AdminModel> objAdminlst { get; set; }
}
----Controller
[HttpGet]
public ActionResult AddEditUserType(int UserTypeID)
{
AdminModel objFormAdminModel = new AdminModel();
if (UserTypeID != -1)
{
objFormAdminModel = GetRecord(UserTypeID);
objFormAdminModel.Status = "Edit";
}
else
{
objFormAdminModel.Status = "Add";
}
return View(objFormAdminModel);
}
[HttpPost]
public ActionResult AddEditUserType(AdminModel objFormAdminModel)
{
AdminModel objAdminModel = new AdminModel();
List<string> lstUserTypes = (from userType in objPNetContext.UserTypes
select userType.Description).ToList<string>();
List<int> lstUserTypes1 = (from userType in objPNetContext.UserTypes
select userType.UserTypeID).ToList<int>();
if (objFormAdminModel.Status == "Add")
{
if (!lstUserTypes.Contains(objFormAdminModel.PDescription))
{
objAdminModel.PDescription = objFormAdminModel.PDescription;
AddUserTypeInsert(objFormAdminModel);
}
}
else if (objFormAdminModel.Status == "Edit")
{
if (!lstUserTypes1.Contains(objFormAdminModel.PUserTypeID))
{
objAdminModel.PUserTypeID = objFormAdminModel.PUserTypeID;
objAdminModel.PDescription = objFormAdminModel.PDescription;
UserTypeUpdateFunction(objAdminModel);
}
}
objAdminModel.PResult = "UserType already exist.";
return View(objAdminModel);
}
----View
@foreach (var item in Model.objAdminlst)
{
@item.PDescription
<img width="15px" alt="" height="15px" src="@Url.Content("~/Content/Images/edit.gif")" />
style="font-family: Verdana; font-size: small;">Edit
<img width="15px" alt="" height="15px" src="@Url.Content("~/Content/Images/delete.gif")" önclick="javascript:return ConfirmationBox();"/>Delete
}