Click here to Skip to main content
13,046,176 members (58,613 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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

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; }

public ActionResult AddEditUserType(int UserTypeID)
AdminModel objFormAdminModel = new AdminModel();
if (UserTypeID != -1)
objFormAdminModel = GetRecord(UserTypeID);
objFormAdminModel.Status = "Edit";
objFormAdminModel.Status = "Add";
return View(objFormAdminModel);
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;
else if (objFormAdminModel.Status == "Edit")
if (!lstUserTypes1.Contains(objFormAdminModel.PUserTypeID))
objAdminModel.PUserTypeID = objFormAdminModel.PUserTypeID;
objAdminModel.PDescription = objFormAdminModel.PDescription;


objAdminModel.PResult = "UserType already exist.";

return View(objAdminModel);
@foreach (var item in Model.objAdminlst)


<img width="15px" alt="" height="15px" src="@Url.Content("~/Content/Images/edit.gif")" />
style="font-family: Verdana; font-size: small;">

<img width="15px" alt="" height="15px" src="@Url.Content("~/Content/Images/delete.gif")" önclick="javascript:return ConfirmationBox();"/>Delete

Posted 5-Mar-13 1:14am
Updated 5-Mar-13 1:17am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100