using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
namespace DataLayer
{
public class EmployeeBusinessLayer
{
public IEnumerable<Employee> employees
{
get
{
// for this section code please refer previous example for access the
// employee data from database
}
}
public void AddEmployee(Employee emp)
{
string conString = ConfigurationManager.ConnectionStrings["EmpContext"].ConnectionString;
int empID = emp.EmpID;
string empName = emp.Name;
string empgender = emp.Gender;
double empSalary = emp.Salary;
string empAddress = emp.Address;
string strInsert = "insert into tblemp values("+empID+",'"+empName+"','"+empgender+"',"+empSalary+",'"+empAddress+"')";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(strInsert, con);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
using DataLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace MvcBusinessObject.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Index()
{
EmployeeBusinessLayer empBusLayer = new EmployeeBusinessLayer();
List<Employee> empList= empBusLayer.employees.ToList();
return View(empList);
}
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(FormCollection formColl)
{
Employee empObj = new Employee();
empObj.EmpID = Convert.ToInt32(formColl["empID"]);
empObj.Name = formColl["Name"];
empObj.Gender = formColl["Gender"];
empObj.Salary =Convert.ToDouble( formColl["Salary"]);
empObj.Address = formColl["Address"];
EmployeeBusinessLayer obj = new EmployeeBusinessLayer();
obj.AddEmployee(empObj);
return RedirectToAction("Index");
}
}
}
@model DataLayer.Employee
@{
ViewBag.Title = "Create";
}
<h2>Add new Employee</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Employee</legend>
<div class="editor-label">
@Html.LabelFor(model => model.EmpID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmpID)
@Html.ValidationMessageFor(model => model.EmpID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Gender)
</div>
<div class="editor-field">
@Html.DropDownList("Gender", new List<SelectListItem>{
new SelectListItem { Text = "Male", Value = "Male" },
new SelectListItem { Text = "Female", Value = "Female" } },
"Select Gender")
@Html.ValidationMessageFor(model => model.Gender)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Salary)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Salary)
@Html.ValidationMessageFor(model => model.Salary)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("∼/bundles/jqueryval")
}
@Html.EditorFor(model => model.Gender)
with
@Html.DropDownList("Gender", new List<SelectListItem>
{
new SelectListItem { Text = "Male", Value="Male" },
new SelectListItem { Text = "Female", Value="Female" }
}, "Select Gender")
[HttpPost]
public ActionResult Create(Employee empObj)
{
if (ModelState.IsValid)
{
EmployeeBusinessLayer obj = new EmployeeBusinessLayer();
obj.AddEmployee(empObj);
return RedirectToAction("Index");
}
return View();
}
[HttpPost]
[ActionName("Create")]
public ActionResult CreatePost()
{
if (ModelState.IsValid)
{
Employee empObj = new Employee();
UpdateModel(empObj);
EmployeeBusinessLayer obj = new EmployeeBusinessLayer();
obj.AddEmployee(empObj);
return RedirectToAction("Index");
}
return View();
}