1. Add a controller as "Create"
In order to use as input form, it needs to be put "[HttpGet]"
2. Add a view on right click in create controller > create a strongly-typed view (Employee (BusinessLayerRody) > Scaffold template : Create
3. In order to connect the action of "Create" button, it needs to add a business logic in "EmployeeBusinessLayer.cs"
public void AddEmployee(Employee employee)
{
string connectionString = ConfigurationManager.ConnectionStrings["employeeDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spAddEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramName = new SqlParameter();
paramName.ParameterName = "@Name";
paramName.Value = employee.Name;
cmd.Parameters.Add(paramName);
SqlParameter paramGender = new SqlParameter();
paramGender.ParameterName = "@Gender";
paramGender.Value = employee.Gender;
cmd.Parameters.Add(paramGender);
SqlParameter paramCity = new SqlParameter();
paramCity.ParameterName = "@City";
paramCity.Value = employee.City;
cmd.Parameters.Add(paramCity);
SqlParameter paramDateOfBirth = new SqlParameter();
paramDateOfBirth.ParameterName = "@DateOfBirth";
paramDateOfBirth.Value = employee.DateOfBirth;
cmd.Parameters.Add(paramDateOfBirth);
con.Open();
cmd.ExecuteNonQuery();
}
}
4. Add a post action in "EmployeeController.ca"
[HttpPost]
public ActionResult Create(FormCollection formCollection)
{
Employee employee = new Employee();
employee.Name = formCollection["Name"];
employee.Gender = formCollection["Gender"];
employee.City = formCollection["City"];
employee.DateOfBirth = Convert.ToDateTime(formCollection["DateOfBirth"]);
EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
employeeBusinessLayer.AddEmployee(employee);
return RedirectToAction("Index");
}
OR
[HttpPost]
public ActionResult Create(string name, string gender, string city, DateTime dateOfBirth)
{
Employee employee = new Employee();
employee.Name = name;
employee.Gender = gender;
employee.City = city;
employee.DateOfBirth = Convert.ToDateTime(dateOfBirth);
EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
employeeBusinessLayer.AddEmployee(employee);
return RedirectToAction("Index");
}
OR
[HttpPost]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
employeeBusinessLayer.AddEmployee(employee);
return RedirectToAction("Index");
}
return View();
}
OR
[HttpPost]
public ActionResult Create()
{
if (ModelState.IsValid)
{
Employee employee = new Employee();
UpdateModel(employee);
EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
employeeBusinessLayer.AddEmployee(employee);
return RedirectToAction("Index");
}
return View();
}
5. In order to validate the form when "create" button click, add a reference "EF" on both solution, and then put DataAnnotation as using
using System.ComponentModel.DataAnnotations;
public class Employee
{
public int Id { get; set; }
[Required] //EF
public string Name { get; set; }
[Required]
public string Gender { get; set; }
[Required]
public string City { get; set; }
[Required]
public DateTime? DateOfBirth { get; set; }
}
if the validation message is changed, you need to change "[Required]" to "[Require(ErrorMessage="Please fill this value")]"
and check HTML on cshtml file.
@Html.ValidationMessageFor(model => model.Gender)