Click here to Skip to main content
11,712,241 members (70,889 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server MVC3
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 lstUserTypes = (from userType in objPNetContext.UserTypes
select userType.Description).ToList();
List lstUserTypes1 = (from userType in objPNetContext.UserTypes
select userType.UserTypeID).ToList();


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

}
Posted 5-Mar-13 1:14am
DGKumar802
Edited 5-Mar-13 1:17am
v2

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

  Print Answers RSS
0 CHill60 335
1 Sergey Alexandrovich Kryukov 244
2 OriginalGriff 230
3 Andy Lanng 160
4 Maciej Los 130
0 CHill60 335
1 Sergey Alexandrovich Kryukov 245
2 OriginalGriff 230
3 Andy Lanng 160
4 Maciej Los 130


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 5 Mar 2013
Copyright © CodeProject, 1999-2015
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