Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.16 KB | None | 0 0
  1. @model WebApplication1.Models.Employee
  2. @{
  3. ViewBag.Title = "Index";
  4. Layout = "~/Views/Shared/_Layout.cshtml";
  5. }
  6. <!DOCTYPE html>
  7. @section Scripts {
  8. @Scripts.Render("~/bundles/jqueryval")
  9. }
  10. <html>
  11. <head>
  12. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
  13. <link href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css" rel="stylesheet" />
  14. <link rel="stylesheet" href="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" />
  15.  
  16. <meta name="viewport" content="width=device-width" />
  17. <title>Create</title>
  18. </head>
  19. <body>
  20. <div class="register_form full_width_reg">
  21. @using (Html.BeginForm("Insertdata", "Employee", FormMethod.Post, new { @class = "form-horizontal", @id = "insertform", role = "form", enctype = "multipart/form-data" }))
  22. {
  23.  
  24. <div id="addupdate"></div>
  25. }
  26. </div>
  27. <div id="grid">
  28. </div>
  29. </body>
  30. </html>
  31. @*<script src="https://code.jquery.com/jquery-3.3.1.js"></script>*@
  32. <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  33. <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
  34. <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
  35. <script type="text/javascript" src="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>
  36. <script>
  37. $(document).ready(function () {
  38. $.ajax({
  39. type: "GET",
  40. url: "/Employee/GetAddUpdate",
  41. contentType: "application/json; charset=utf-8",
  42. dataType: "html",
  43. success: function (response) {
  44. if (response != null) {
  45. $("#addupdate").html(response);
  46. $('#datetimepicker1').datetimepicker();
  47. $.validator.unobtrusive.parse("#insertform");
  48. } else {
  49. alert("Something went wrong");
  50. }
  51. },
  52. failure: function (response) {
  53. alert(response.responseText);
  54. },
  55. error: function (response) {
  56. alert(response.responseText);
  57. }
  58. });
  59. bindgrid();
  60. });
  61.  
  62. function bindgrid() {
  63. $.ajax({
  64. type: "GET",
  65. url: "/Employee/Getpartialdata",
  66. contentType: "application/json; charset=utf-8",
  67. dataType: "html",
  68. success: function (response) {
  69. if (response != null) {
  70. $("#grid").html(response);
  71. $('#example').DataTable();
  72. } else {
  73. alert("Something went wrong");
  74. }
  75. },
  76. failure: function (response) {
  77. alert(response.responseText);
  78. },
  79. error: function (response) {
  80. alert(response.responseText);
  81. }
  82. });
  83. }
  84.  
  85. function insert() {
  86. var cont = 0;
  87. $('input:radio[name="gender"]').change(
  88. function () {
  89. if ($(this).is(':checked')) {
  90. cont = cont + 1;
  91. }
  92. });
  93.  
  94. if ($("#txtname").val() == '' || $("#txtemail").val() == '' || $("#datetimepicker7").val() == '' || $("#designationid").val() == ''
  95. || $("#txtsalary").val() == '' || $("#designationid").val() == '' || cont==0) {
  96. $("#validate").click();
  97. return false;
  98. }
  99. $.ajax({
  100. type: "GET",
  101. url: "/Employee/Insertdata",
  102. contentType: "application/json; charset=utf-8",
  103. dataType: "json",
  104. data: $("#insertform").serialize(),
  105. success: function (response) {
  106. bindgrid();
  107. },
  108. failure: function (response) {
  109. alert(response.responseText);
  110. },
  111. error: function (response) {
  112. alert(response.responseText);
  113. }
  114. });
  115. }
  116. </script>
  117.  
  118. @model WebApplication1.Models.Employee
  119. @Html.HiddenFor(m => m.empid)
  120. @Html.ValidationSummary(true)
  121. <h3>Register as a Student</h3>
  122. <hr />
  123. <div class="hidden"></div>
  124.  
  125. <div class="form-group col-md-6 col-sm-6">
  126. <div class="row">
  127. <label class="col-md-4 control-label" for="Name">Name</label>
  128. <div class="col-md-8">
  129. @Html.EditorFor(model => model.empname, new { htmlAttributes = new { @class = "form-control", id = "txtname", placeholder = "Name" } })
  130. @Html.ValidationMessageFor(model => model.empname)
  131. </div>
  132. </div>
  133. </div>
  134. <div class="form-group col-md-6 col-sm-6">
  135. <div class="row">
  136. <label class="col-md-4 control-label" for="Campus">Designation</label>
  137. <div class="col-md-8">
  138. @Html.DropDownListFor(model => model.designationid, Model.AvailableDesig, new { @class = "form-control" })
  139. @Html.ValidationMessageFor(model => model.designationid)
  140. </div>
  141. </div>
  142. </div>
  143. <div class="clearfix"></div>
  144. <div class="form-group col-md-6 col-sm-6">
  145. <div class="row">
  146. <label class="col-md-4 control-label" for="Phone">Phone</label>
  147. <div class="col-md-8">
  148. @Html.EditorFor(model => model.empemail, new { htmlAttributes = new { @class = "form-control", id = "txtemail", placeholder = "Email Address" } })
  149. @Html.ValidationMessageFor(model => model.empemail)
  150. </div>
  151. </div>
  152. </div>
  153. <div class="form-group col-md-6 col-sm-6">
  154. <div class="row">
  155. @Html.LabelFor(m => m.empsalary, new { @class = "col-md-4 control-label" })
  156. <div class="col-md-8">
  157. @Html.EditorFor(model => model.empsalary, new { htmlAttributes = new { @class = "form-control", id = "txtsalary", placeholder = "Salary" } })
  158. @Html.ValidationMessageFor(model => model.empsalary)
  159.  
  160. </div>
  161. </div>
  162. </div>
  163. <div class="clearfix"></div>
  164.  
  165. <div class="form-group col-md-6 col-sm-6">
  166. <div class="row">
  167. @Html.LabelFor(m => m.date, new { @class = "col-md-4 control-label" })
  168. <div class="col-md-8">
  169. @Html.EditorFor(m => m.date, new { htmlAttributes = new { @class = "form-control input-group date", id = "datetimepicker1", placeholder = "Date" } })
  170. @Html.ValidationMessageFor(model => model.date)
  171. </div>
  172. </div>
  173. </div>
  174.  
  175. <div class="form-group col-md-6 col-sm-6">
  176. <div class="row">
  177. @Html.LabelFor(m => m.gender, new { @class = "col-md-4 control-label" })
  178. <div class="col-md-8">
  179. @Html.Label("Male")
  180. @Html.RadioButtonFor(m => m.gender, "Male")
  181. @Html.Label("Female")
  182. @Html.RadioButtonFor(m => m.gender, "Female")
  183. @Html.ValidationMessageFor(model => model.gender)
  184. </div>
  185. </div>
  186. </div>
  187. <div class="form-group col-md-6 col-sm-6">
  188. <div class="row">
  189. <div class="col-md-offset-4 col-md-10">
  190. <input type="submit" id="validate" class="btn btn-default btn-browse" value="Add" style="display:none;" />
  191. <input type="button" id="submitfilteration" onclick="return insert();" class="btn btn-default btn-browse" value="Add" />
  192. </div>
  193. </div>
  194. </div>
  195.  
  196. @model IList<WebApplication1.Models.Employee>
  197.  
  198. <table id="example" class="table table-striped table-bordered" style="width:100%">
  199. <thead>
  200. <tr>
  201. <th>Id</th>
  202. <th>Name</th>
  203. <th>Email</th>
  204. <th>Gender</th>
  205. <th>Designation</th>
  206. <th>Salary</th>
  207. <th>Date</th>
  208. <th>Edit</th>
  209. <th>Delete</th>
  210. </tr>
  211. </thead>
  212. <tbody>
  213. @foreach (var item in Model)
  214. {
  215.  
  216. <tr>
  217. <td>@item.id</td>
  218. <td>@item.empname</td>
  219. <td>@item.empemail</td>
  220. <td>@item.gender</td>
  221. <td>@item.name</td>
  222. <td>@item.empsalary</td>
  223. <td>@item.date</td>
  224. <td>
  225. <input type="button" id="Editbtn" onclick="Editdata('@item.empid')" class="btn btn-default btn-browse" value="Edit" />
  226. </td>
  227. <td>
  228. <input type="button" id="deletebutton" onclick="Delete('@item.empid');" class="btn btn-default btn-browse" value="Delete" />
  229. </td>
  230. </tr>
  231. }
  232. </tbody>
  233. </table>
  234. <script>
  235. function Delete(id) {
  236. alert(id);
  237. $.ajax({
  238. url: '/Employee/Delete',
  239. dataType: "html",
  240. type: "POST", // You can use GET
  241. contentType: 'application/json; charset=utf-8',
  242. cache: false,
  243. data: JSON.stringify({
  244. 'id': id
  245. }),
  246. success: function (data) {
  247. alert(data);
  248. bindgrid();
  249. alert('success');
  250. },
  251. error: function (xhr) {
  252. console.log(request);
  253. }
  254. });
  255. }
  256. function Editdata(id) {
  257. $.ajax({
  258. url: '/Employee/Edit',
  259. dataType: "html",
  260. type: "POST", // You can use GET
  261. contentType: 'application/json; charset=utf-8',
  262. cache: false,
  263. data: JSON.stringify({
  264. 'id': id
  265. }),
  266. success: function (data) {
  267. $("#addupdate").html(data);
  268. $.validator.unobtrusive.parse("#insertform");
  269. alert('success');
  270. },
  271. error: function (xhr) {
  272. console.log(request);
  273. // alert("Some error");
  274. }
  275. });
  276. }
  277. function bindgrid() {
  278. $.ajax({
  279. type: "GET",
  280. url: "/Employee/Getpartialdata",
  281. contentType: "application/json; charset=utf-8",
  282. dataType: "html",
  283. success: function (response) {
  284. if (response != null) {
  285. $("#grid").html(response);
  286. $('#example').DataTable();
  287. } else {
  288. alert("Something went wrong");
  289. }
  290. },
  291. failure: function (response) {
  292. alert(response.responseText);
  293. },
  294. error: function (response) {
  295. alert(response.responseText);
  296. }
  297. });
  298. }
  299. </script>
  300.  
  301. public class EmployeeController : Controller
  302. {
  303. PracmvcEntities db = new PracmvcEntities();
  304. // GET: Employee
  305. public ActionResult Index()
  306. {
  307. Employee emp = new Employee();
  308. emp.AvailableDesig = GetDesig();
  309. return View(emp);
  310. }
  311. [HttpGet]
  312. public ActionResult Insertdata(Employee model)
  313. {
  314. bool gender = false;
  315. if (model.empid == null)
  316. {
  317. if (model.gender == "Male")
  318. {
  319. gender = true;
  320. }
  321. db.insertemployeedata(model.empname, gender, model.designationid, model.empemail, model.empsalary, model.date);
  322. db.SaveChanges();
  323. return Json("success", JsonRequestBehavior.AllowGet);
  324. }
  325. //edit
  326. if (model.gender == "Male")
  327. {
  328. gender = true;
  329. }
  330. db.updateemployeedata(model.empid,model.empname, gender, model.designationid, model.empemail, model.empsalary, model.date);
  331. db.SaveChanges();
  332. return Json("success", JsonRequestBehavior.AllowGet);
  333. }
  334.  
  335. [HttpGet]
  336. public ActionResult GetAddUpdate()
  337. {
  338. Employee emp = new Employee();
  339. emp.AvailableDesig = GetDesig();
  340. return PartialView("_AddUpdateData", emp);
  341. }
  342. [HttpGet]
  343. public ActionResult Getpartialdata()
  344. {
  345. List<Employee> emp = db.selectemployeedata(0).Select(e => new Employee
  346. {
  347. empid = e.empid,
  348. empname = e.empname,
  349. empsalary = Convert.ToDecimal(e.empsalary),
  350. empemail = e.empemail,
  351. gender = e.gender.ToString(),
  352. name = db.designations.Where(a => a.id == e.designationid).FirstOrDefault().name,
  353. id=e.id,
  354. date=e.date
  355. }).ToList();
  356. return PartialView("_PartialGrid", emp);
  357. }
  358. public IList<SelectListItem> GetDesig()
  359. {
  360. // This comes from database.
  361. var desig = db.selectemployeeedesignation()
  362. .Select(x => new SelectListItem { Text = x.name, Value = x.id.ToString() })
  363. .ToList();
  364. desig.Insert(0, new SelectListItem { Text = "--Please Select--", Value = "" });
  365. return desig;
  366. }
  367. [HttpPost]
  368. public ActionResult Edit(int id)
  369. {
  370. Employee emp = db.selectemployeedata(id).Select(e => new Employee
  371. {
  372. empid = e.empid,
  373. empname = e.empname,
  374. empsalary = Convert.ToDecimal(e.empsalary),
  375. empemail = e.empemail,
  376. gender = e.gender.ToString()=="True"? "Male" : "Female",
  377. name = db.designations.Where(a => a.id == e.designationid).FirstOrDefault().name,
  378. id = e.id,
  379. date = e.date,
  380. designationid=e.designationid
  381. }).FirstOrDefault();
  382. emp.AvailableDesig = GetDesig();
  383. return PartialView("_AddUpdateData", emp);
  384. }
  385. [HttpPost]
  386. public ActionResult Delete(int id)
  387. {
  388. db.deleteemployee(id);
  389. db.SaveChanges();
  390. // This comes from database.
  391. return Json("success", JsonRequestBehavior.AllowGet);
  392. }
  393.  
  394. }
  395.  
  396. public Employee()
  397. {
  398. AvailableDesig = new List<SelectListItem>();
  399. }
  400. public int? empid { get; set; }
  401. [Required(ErrorMessage = "Name Required.")]
  402. public string empname { get; set; }
  403. [Required(ErrorMessage = "Gender Required.")]
  404. public string gender { get; set; }
  405. [Required(ErrorMessage = "Please Select {0}.")]
  406. public Nullable<int> designationid { get; set; }
  407. [Required(ErrorMessage = "Email Address Required.")]
  408. public string empemail { get; set; }
  409. [Required(ErrorMessage = "Salary Required.")]
  410. public Nullable<System.Decimal> empsalary { get; set; }
  411. [Required(ErrorMessage = "Date Required.")]
  412. public Nullable<System.DateTime> date { get; set; }
  413. public int id { get; set; }
  414. public string name { get; set; }
  415. public IList<SelectListItem> AvailableDesig { get; set; }
  416. public IList<Employeedesignation> desig { get; set; }
  417. }
  418. public class Employeedesignation
  419. {
  420. public int id { get; set; }
  421. public string name { get; set; }
  422. }
  423.  
  424. <head>
  425. <meta charset="utf-8" />
  426. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  427. <title>@ViewBag.Title - My ASP.NET Application</title>
  428. @Styles.Render("~/Content/css")
  429. @Scripts.Render("~/bundles/jquery")
  430. @Scripts.Render("~/bundles/bootstrap")
  431. @RenderSection("scripts", required: false)
  432. @Scripts.Render("~/bundles/modernizr")
  433. </head>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement