Click here to Skip to main content
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 2:14am
DGKumar759
Edited 5-Mar-13 2: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 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 5 Mar 2013
Copyright © CodeProject, 1999-2014
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