Manoj Maharana

Manoj Maharana

  • NA
  • 362
  • 128k

mvc basic crud operations using .net mvc

Aug 6 2017 4:32 AM
User.cs:
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<tblUser> 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<tblUser> 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<tblSetting> 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<tblSetting> 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<UserModel>();
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<UserModel>();
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<UserModel>();
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<UserModel>();
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<UserModel>();
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<UserModel>();
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<UserModel>();
ManageUserSettingList = new List<UserModel>();
ManageMapSettingList = new List<UserModel>();
}
public List<UserModel> ManageUserList { get; set; }
public List<UserModel> ManageUserSettingList { get; set; }
public List<UserModel> 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";
}
<div class="box-body">
<div class="row">
<div class="col-sm-6">
<a href="@Url.Action("AddUser", "User", new { })" class="btn btn-primary">Add New User</a>
</div>
</div>
</div>
<table id="tbUnit" class="table table-bordered tablesorter table-striped">
<thead class="panel panel-default">
<tr class="panel-heading">
<th class="text-center">Action</th>
<th class="text-center">
ID
</th>
<th class="text-center">
User
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ManageUserList)
{
<tr>
<td class="text-center">
<a href="@Url.Action("Edit", "User", new { id = item.UserId })" class="text-warning" data-toggle="tooltip" title="Edit">Edit</a>
<a class="delete" href="@Url.Action("DeleteUser", "User", new { id = item.UserId, unit_all = 1 })" onclick="return confirm('Are you sure want to delete?')" data-toggle="tooltip" title="Delete">Delete</a>
</td>
<td class="text-center">
@item.UserId
</td>
<td class="text-center">
@item.UserName
</td>
</tr>
}
</tbody>
</table>
<div class="box-body">
<div class="row">
<div class="col-sm-6">
<a href="@Url.Action("AddSetting", "User", new { })" class="btn btn-primary">Add New Setting</a>
</div>
</div>
</div>
<table id="tbUnit" class="table table-bordered tablesorter table-striped">
<thead class="panel panel-default">
<tr class="panel-heading">
<th class="text-center">Action</th>
<th class="text-center">
ID
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
CreatedBy
</th>
<th class="text-center">
ApprovedBy
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ManageUserSettingList)
{
<tr>
<td class="text-center">
<a href="@Url.Action("EditSetting", "User", new { id = item.Id })" class="text-warning" data-toggle="tooltip" title="Edit">Edit</a>
<a class="delete" href="@Url.Action("DeleteSetting", "User", new { id = item.Id, unit_all = 1 })" onclick="return confirm('Are you sure want to delete?')" data-toggle="tooltip" title="Delete">Delete</a>
</td>
<td class="text-center">
@item.Id
</td>
<td class="text-center">
@item.Name
</td>
<td class="text-center">
@item.CreatedBy
</td>
<td class="text-center">
@item.ApprovedBy
</td>
</tr>
}
</tbody>
</table>
_____________________________________
AddUser.cs:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "AddUser";
}
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Add</h3>
</div><!-- /.box-header -->
@using (Html.BeginForm())
{
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>ID </label>
@Html.TextBoxFor(model => model.UserId, new { @class = "form-control", @disabled = "disabled" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>User* </label>
@Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })
<span id="errorSelectFacility" style="color: Red;"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Password* </label>
@Html.TextBoxFor(model => model.Password, new { @class = "form-control", @type = "password" })
<span id="errorSelectFacility" style="color: Red;"></span>
</div>
</div>
</div>
<br />
</div><!-- /.box-body -->
<div class="clearfix"></div>
<div class="box-footer text-center">
<button type="submit" class="btn btn-primary" onclick="return validateForm();">Submit</button>
</div>
@* </form>*@
}
</div><!-- /.box -->
</div>
</div>
</section>
_________________________________________
User.cshtml:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "Edit";
}
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Add</h3>
</div><!-- /.box-header -->
@using (Html.BeginForm())
{
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>ID </label>
@Html.TextBoxFor(model => model.UserId, new { @class = "form-control", @disabled = "disabled" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>User* </label>
@Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })
<span id="errorSelectFacility" style="color: Red;"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Password* </label>
@Html.TextBoxFor(model => model.Password, new { @class = "form-control", @type = "password" })
<span id="errorSelectFacility" style="color: Red;"></span>
</div>
</div>
</div>
<br />
</div><!-- /.box-body -->
<div class="clearfix"></div>
<div class="box-footer text-center">
<button type="submit" class="btn btn-primary" onclick="return validateForm();">Submit</button>
</div>
@* </form>*@
}
</div><!-- /.box -->
</div>
</div>
</section>
______________________
AddEditsetting.cshtml:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "AddSetting";
}
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Add</h3>
</div><!-- /.box-header -->
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.hdnCreatedBy)
@Html.HiddenFor(m => m.hdnApprovedBy)
<!-- form start -->
@*<form role="form">*@
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>ID </label>
@Html.TextBoxFor(model => model.Id, new { @class = "form-control", @disabled = "disabled" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Name* </label>
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
<span id="errorSelectFacility" style="color: Red;"></span>
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-md-12">
<table id="tbDoc" class="table table-bordered tablesorter table-striped" style="margin-bottom: 0; overflow-x: auto;">
<thead class="panel panel-default">
</thead>
<tbody>
<tr>
<td>
CreatedBy
</td>
@foreach (var item in Model.ManageUserList)
{
<td class="text-center">
<input type="checkbox" onchange="AddCreatedbyid(this,'@item.UserId')" id="@item.UserId">@item.UserName
</td>
}
</tr>
</tbody>
</table>
<table id="tbDoc" class="table table-bordered tablesorter table-striped" style="margin-bottom: 0; overflow-x: auto;">
<thead class="panel panel-default">
</thead>
<tbody>
<tr>
<td>
ApprovedBy
</td>
@foreach (var item in Model.ManageUserList)
{
<td class="text-center">
<input type="checkbox" onchange="AddApprovedbyid(this,'@item.UserId')" id="@item.UserId">@item.UserName
</td>
}
</tr>
</tbody>
</table>
</div>
</div>
</div><!-- /.box-body -->
<div class="clearfix"></div>
<div class="box-footer text-center">
<button type="submit" class="btn btn-primary" onclick="return validateForm();">Submit</button>
</div>
@* </form>*@
}
</div><!-- /.box -->
</div>
</div>
</section>
<script>
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;
}
}
</script>
__________________
EditSetting.cshtml:
@model UserSetting.Models.UserModel
@{
ViewBag.Title = "EditSetting";
}
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Add</h3>
</div><!-- /.box-header -->
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.hdnCreatedBy)
@Html.HiddenFor(m => m.hdnApprovedBy)
<!-- form start -->
@*<form role="form">*@
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>ID </label>
@Html.TextBoxFor(model => model.Id, new { @class = "form-control", @disabled = "disabled" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Name* </label>
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
<span id="errorSelectFacility" style="color: Red;"></span>
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-md-12">
<table id="tbDoc" class="table table-bordered tablesorter table-striped" style="margin-bottom: 0; overflow-x: auto;">
<thead class="panel panel-default">
</thead>
<tbody>
<tr>
<td>
CreatedBy
</td>
@foreach (var item in Model.ManageUserList)
{
string strValue = Model.CreatedBy;
string[] strArray = strValue.Split(',');
Int32 createdid = 0;
foreach (var created in strArray)
{
if (created != null && created != "")
{
if (Convert.ToInt32(created) == item.UserId)
{
createdid = Convert.ToInt32(created);
break;
}
}
}
<td class="text-center">
<input type="checkbox" onchange="AddCreatedbyid(this,'@item.UserId')" id="@item.UserId" @(Convert.ToInt32(createdid) != 0 ? "checked" : "")>@item.UserName
</td>
}
</tr>
</tbody>
</table>
<table id="tbDoc" class="table table-bordered tablesorter table-striped" style="margin-bottom: 0; overflow-x: auto;">
<thead class="panel panel-default">
</thead>
<tbody>
<tr>
<td>
ApprovedBy
</td>
@foreach (var item in Model.ManageUserList)
{
string strValue = Model.ApprovedBy;
string[] strArray = strValue.Split(',');
Int32 createdid = 0;
foreach (var created in strArray)
{
if (created != null && created != "")
{
if (Convert.ToInt32(created) == item.UserId)
{
createdid = Convert.ToInt32(created);
break;
}
}
}
<td class="text-center">
<input type="checkbox" onchange="AddApprovedbyid(this,'@item.UserId')" id="@item.UserId" @(Convert.ToInt32(createdid) != 0 ? "checked" : "")>@item.UserName
</td>
}
</tr>
</tbody>
</table>
</div>
</div>
</div><!-- /.box-body -->
<div class="clearfix"></div>
<div class="box-footer text-center">
<button type="submit" class="btn btn-primary" onclick="return validateForm();">Submit</button>
</div>
@* </form>*@
}
</div><!-- /.box -->
</div>
</div>
</section>
<script>
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;
}
}
</script>

Answers (2)