Mvc simple insertion for brginers practice coding list
@using Entities;@model DTO.EmpModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>EmpCategory</title>
</head>
<body>
@using (Html.BeginForm("EmpCategory", "Employee", FormMethod.Post))
{
<div>
<table>
<tr>
<td><b>Employee Category:</b></td>
<td>
@Html.TextAreaFor(model => model.EmpCategory)
</td>
</tr>
<tr>
<td><b>Employee Name:</b></td>
<td>
@Html.TextAreaFor(model => model.Empname)
</td>
</tr>
<tr>
<td><b>Prefix :</b></td>
<td>
@Html.TextAreaFor(model => model.Prefix)
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit" /></td>
</tr>
</table>
</div>
}
@using (Html.BeginForm("EmpCategory", "Employee", FormMethod.Get))
{
<div>
<table>
@foreach (var item in ViewData["EmpDetail"] as IEnumerable<Employee>)
{
<tr>
<td>@item.EmpCategory</td>
</tr>
}
</table>
</div>
}
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTO;
using Entities;
using AutoMapper;
namespace WebApp.Controllers
{
public class EmployeeController : Controller
{
LocTestEntities3 objContext = new LocTestEntities3();
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult EmpCategory()
{
List<Employee> emp = objContext.Employees.ToList();
ViewData["EmpDetail"] = emp;
return View();
}
[HttpPost]
public ActionResult EmpCategory(EmpModel Emp)
{
if (ModelState.IsValid)
{
Mapper.CreateMap<EmpModel, Employee>().ForMember(t => t.EmpId, opt => opt.Ignore());
var empdetails = Mapper.Map<EmpModel, Employee>(Emp);
objContext.Employees.Add(empdetails);
objContext.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
}
}