using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity.Validation;
namespace UserSetting.DB
{
public class User
{
CorpoInvoiceEntities db = new CorpoInvoiceEntities();
public List GetUser(int id)
{
using (CorpoInvoiceEntities db = new CorpoInvoiceEntities())
{
if (id > 0)
return db.tblUser.Where(x => x.UserId == id).ToList();
else
return db.tblUser.ToList();
}
}
public int Insert_tblUser(tblUser item)
{
int res = 0;
try
{
using (CorpoInvoiceEntities db = new CorpoInvoiceEntities())
{
tblUser model = new tblUser();
model.UserName = item.UserName;
model.Password = item.Password;
db.tblUser.Add(model);
db.SaveChanges();
res = Convert.ToInt32(model.UserId);
}
}
catch (Exception ex)
{
throw ex;
}
return res;
}
public int Update_tblUser(tblUser item)
{
int res = 0;
try
{
using (CorpoInvoiceEntities db = new CorpoInvoiceEntities())
{
tblUser model = (from s in db.tblUser
where s.UserId == item.UserId
select s).FirstOrDefault();
if (model != null)
{
model.UserName = item.UserName;
model.Password = item.Password;
db.SaveChanges();
res = Convert.ToInt32(model.UserId);
}
}
}
catch (Exception ex)
{
throw ex;
}
return res;
}
public List Delete_tblUser(int id)
{
try
{
using (var db = new CorpoInvoiceEntities())
{
tblUser item = db.tblUser.Where(x => x.UserId == id).FirstOrDefault();
db.tblUser.Remove(item);
db.SaveChanges();
return db.tblUser.ToList();
}
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}
public List GetSetting(int id)
{
using (CorpoInvoiceEntities db = new CorpoInvoiceEntities())
{
if (id > 0)
return db.tblSetting.Where(x => x.Id == id).ToList();
else
return db.tblSetting.ToList();
}
}
public int Insert_tblSetting(tblSetting item)
{
int res = 0;
try
{
using (CorpoInvoiceEntities db = new CorpoInvoiceEntities())
{
tblSetting model = new tblSetting();
model.Id = item.Id;
model.Name = item.Name;
model.CreatedBy = item.CreatedBy;
model.ApprovedBy = item.ApprovedBy;
db.tblSetting.Add(model);
db.SaveChanges();
res = Convert.ToInt32(model.Id);
}
}
catch (Exception ex)
{
throw ex;
}
return res;
}
public int Update_tblSetting(tblSetting item)
{
int res = 0;
try
{
using (CorpoInvoiceEntities db = new CorpoInvoiceEntities())
{
tblSetting model = (from s in db.tblSetting
where s.Id == item.Id
select s).FirstOrDefault();
if (model != null)
{
model.Name = item.Name;
model.CreatedBy = item.CreatedBy;
model.ApprovedBy = item.ApprovedBy;
db.SaveChanges();
res = Convert.ToInt32(model.Id);
}
}
}
catch (Exception ex)
{
throw ex;
}
return res;
}
public List Delete_tblSetting(int id)
{
try
{
using (var db = new CorpoInvoiceEntities())
{
tblSetting item = db.tblSetting.Where(x => x.Id == id).FirstOrDefault();
db.tblSetting.Remove(item);
db.SaveChanges();
return db.tblSetting.ToList();
}
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}
}
}
________________________
UserController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UserSetting.Models;
using UserSetting.DB;
namespace UserSetting.Controllers
{
public class UserController : Controller
{
//
// GET: /User/
UserModel model = new UserModel();
User obj = new User();
public ActionResult Index()
{
UserModel model = new UserModel();
var selectManageUser = new List();
var Userlist = obj.GetUser(0).ToList();
selectManageUser = Userlist.Select(item =>
{
return new UserModel()
{
UserId = Convert.ToInt32(item.UserId),
UserName = item.UserName,
};
}).ToList();
model.ManageUserList = selectManageUser;
var selectManageSetting = new List();
var settinglist = obj.GetSetting(0).ToList();
selectManageSetting = settinglist.Select(item =>
{
return new UserModel()
{
Id = Convert.ToInt32(item.Id),
Name = item.Name,
CreatedBy = item.CreatedBy,
ApprovedBy = item.ApprovedBy,
};
}).ToList();
model.ManageUserSettingList = selectManageSetting;
return View(model);
}
public ActionResult AddUser()
{
UserModel model = new UserModel();
return View(model);
}
[HttpPost]
public ActionResult AddUser(UserModel model)
{
tblUser item = new tblUser();
item.UserName = model.UserName;
item.Password = model.Password;
int ret = obj.Insert_tblUser(item);
return RedirectToAction("Index");
}
public ActionResult Edit(int id)
{
UserModel model = new UserModel();
var selectManageUser = new List();
var Userlist = obj.GetUser(id).FirstOrDefault();
model.UserId = Convert.ToInt32(Userlist.UserId);
model.UserName = Userlist.UserName;
model.Password = Userlist.Password;
return View(model);
}
[HttpPost]
public ActionResult Edit(UserModel model)
{
tblUser item = new tblUser();
item.UserId = Convert.ToInt32(model.Id);
item.UserName = model.UserName;
item.Password = model.Password;
int ret = obj.Update_tblUser(item);
return RedirectToAction("Index");
}
public ActionResult DeleteUser(int id)
{
try
{
var adminUnit = obj.Delete_tblUser(id);
return RedirectToAction("Index", "User");
}
catch (Exception ex)
{
throw ex;
}
}
public ActionResult AddSetting()
{
UserModel model = new UserModel();
var selectManageUser = new List();
var Userlist = obj.GetUser(0).ToList();
selectManageUser = Userlist.Select(item =>
{
return new UserModel()
{
UserId = Convert.ToInt32(item.UserId),
UserName = item.UserName,
};
}).ToList();
model.ManageUserList = selectManageUser;
return View(model);
}
[HttpPost]
public ActionResult AddSetting(UserModel model)
{
tblSetting item = new tblSetting();
item.Name = model.Name;
item.CreatedBy = model.hdnCreatedBy;
item.ApprovedBy = model.hdnApprovedBy;
int ret = obj.Insert_tblSetting(item);
return RedirectToAction("Index");
//return View(model);
}
public ActionResult EditSetting(int id)
{
UserModel model = new UserModel();
var setting = obj.GetSetting(id).FirstOrDefault();
model.Id = Convert.ToInt32(setting.Id);
model.Name = setting.Name;
model.CreatedBy = setting.CreatedBy;
model.ApprovedBy = setting.ApprovedBy;
model.hdnCreatedBy = setting.CreatedBy;
model.hdnApprovedBy = setting.ApprovedBy;
var selectManageSetting = new List();
var settinglist = obj.GetSetting(id).ToList();
selectManageSetting = settinglist.Select(item =>
{
return new UserModel()
{
CreatedBy = item.CreatedBy,
ApprovedBy = item.ApprovedBy,
};
}).ToList();
model.ManageMapSettingList = selectManageSetting;
var selectManageUser = new List();
var Userlist = obj.GetUser(0).ToList();
selectManageUser = Userlist.Select(item =>
{
return new UserModel()
{
UserId = Convert.ToInt32(item.UserId),
UserName = item.UserName,
};
}).ToList();
model.ManageUserList = selectManageUser;
return View(model);
}
[HttpPost]
public ActionResult EditSetting(UserModel model)
{
tblSetting item = new tblSetting();
item.Id = model.Id;
item.Name = model.Name;
item.CreatedBy = model.hdnCreatedBy;
item.ApprovedBy = model.hdnApprovedBy;
int ret = obj.Update_tblSetting(item);
return RedirectToAction("Index");
//return View(model);
}
public ActionResult DeleteSetting(int id)
{
try
{
var adminUnit = obj.Delete_tblSetting(id);
return RedirectToAction("Index", "User");
}
catch (Exception ex)
{
throw ex;
}
}
}
}
__________________________________________
UserModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UserSetting.Models
{
public class UserModel
{
public UserModel()
{
ManageUserList = new List();
ManageUserSettingList = new List();
ManageMapSettingList = new List();
}
public List ManageUserList { get; set; }
public List ManageUserSettingList { get; set; }
public List ManageMapSettingList { get; set; }
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string CreatedBy { get; set; }
public string ApprovedBy { get; set; }
public string hdnCreatedBy { get; set; }
public string hdnApprovedBy { get; set; }
}
}
___________________
Index.cshtml:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "Index";
}
| Action ID User | ||
|---|---|---|
@item.UserId | @item.UserName |
| Action ID Name CreatedBy ApprovedBy | ||||
|---|---|---|---|---|
@item.Id | @item.Name | @item.CreatedBy | @item.ApprovedBy |
_____________________________________
AddUser.cs:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "AddUser";
}
Add
@using (Html.BeginForm())
{
@Html.TextBoxFor(model => model.UserId, new { @class = "form-control", @disabled = "disabled" })
@Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })
@Html.TextBoxFor(model => model.Password, new { @class = "form-control", @type = "password" })
@* *@
}
_________________________________________
User.cshtml:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "Edit";
}
Add
@using (Html.BeginForm())
{
@Html.TextBoxFor(model => model.UserId, new { @class = "form-control", @disabled = "disabled" })
@Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })
@Html.TextBoxFor(model => model.Password, new { @class = "form-control", @type = "password" })
@* *@
}
______________________
AddEditsetting.cshtml:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "AddSetting";
}
Add
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.hdnCreatedBy)
@Html.HiddenFor(m => m.hdnApprovedBy)
@*
@Html.TextBoxFor(model => model.Id, new { @class = "form-control", @disabled = "disabled" })
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
CreatedBy | @item. |
ApprovedBy | @item. |
@* *@
}
function AddCreatedbyid(obj, id) {
//debugger;
var count = 0;
var IdList = document.getElementById("hdnCreatedBy").value;
if (obj.checked) {
document.getElementById("hdnCreatedBy").value = document.getElementById("hdnCreatedBy").value + "," + id;
} else {
var value = document.getElementById("hdnCreatedBy").value;
value = value.replace(id, "");
document.getElementById("hdnCreatedBy").value = value;
}
}
function AddApprovedbyid(obj, id) {
//debugger;
var count = 0;
var IdList = document.getElementById("hdnApprovedBy").value;
if (obj.checked) {
document.getElementById("hdnApprovedBy").value = document.getElementById("hdnApprovedBy").value + "," + id;
} else {
var value = document.getElementById("hdnApprovedBy").value;
value = value.replace(id, "");
document.getElementById("hdnApprovedBy").value = value;
}
}
__________________
EditSetting.cshtml:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "EditSetting";
}
Add
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.hdnCreatedBy)
@Html.HiddenFor(m => m.hdnApprovedBy)
@*
@Html.TextBoxFor(model => model.Id, new { @class = "form-control", @disabled = "disabled" })
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
CreatedBy | @item.UserName |
ApprovedBy | @item.UserName |
@* *@
}
function AddCreatedbyid(obj, id) {
//debugger;
var count = 0;
var IdList = document.getElementById("hdnCreatedBy").value;
if (obj.checked) {
document.getElementById("hdnCreatedBy").value = document.getElementById("hdnCreatedBy").value + "," + id;
} else {
var value = document.getElementById("hdnCreatedBy").value;
value = value.replace(id, "");
document.getElementById("hdnCreatedBy").value = value;
}
}
function AddApprovedbyid(obj, id) {
//debugger;
var count = 0;
var IdList = document.getElementById("hdnApprovedBy").value;
if (obj.checked) {
document.getElementById("hdnApprovedBy").value = document.getElementById("hdnApprovedBy").value + "," + id;
} else {
var value = document.getElementById("hdnApprovedBy").value;
value = value.replace(id, "");
document.getElementById("hdnApprovedBy").value = value;
}
}
Sundaram SubramanianPosted Aug 6, 2017, 9:32 PM
Gokhul VarmanPosted Aug 6, 2017, 9:16 AM