This is What we want to Achieve
Procedure Used
Step 16 : In the Employee Controller for getting the Create New form add Create View.
To Reach to the Index screen of above Screen Shot. Follow below link
Procedure Used
create proc EmployeeTest_Insert
@EmpID int output,
@EmpName varchar(100),
@EmpDesignation varchar(100)
as
begin
Insert EmployeeTest
(
EmpName,
EmpDesignation
)
Values
(
@EmpName,
@EmpDesignation
)
Select @EmpID=Scope_Identity()
end
[HttpGet]
public ActionResult Create()
{
return View();
}
Step 17 : Right click and select Add View
Step 18 : In Create.cshtml presnt inside Employee inside Views
@model
BuisnessLayer.Employee
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.EmpName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmpName)
@Html.ValidationMessageFor(model => model.EmpName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmpDesignation, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmpDesignation)
@Html.ValidationMessageFor(model =>
model.EmpDesignation)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2
col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back
to List", "Index")
</div>
Step 19 : In EmployeeBuisnessLayer Add the following function
public int Employee_Insert(Employee employee)
{
String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
using (SqlConnection sqlcon = new SqlConnection(CS))
{
SqlCommand sqlcmd = new SqlCommand("EmployeeTest_Insert", sqlcon);
sqlcmd.CommandType =
System.Data.CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@EmpName",
employee.EmpName);
sqlcmd.Parameters.AddWithValue("@EmpDesignation",
employee.EmpDesignation);
SqlParameter outPara = new SqlParameter();
outPara.ParameterName = "@EmpID";
outPara.SqlDbType =
System.Data.SqlDbType.Int;
outPara.Direction =
System.Data.ParameterDirection.Output;
sqlcmd.Parameters.Add(outPara);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
string EmpID = outPara.Value.ToString();
return Convert.ToInt16(EmpID);
}
}
Step 20 : for posting the data add the following code in EmployeeController
[HttpPost]
public ActionResult Create(FormCollection formCollection)
{
Employee employee = new Employee();
employee.EmpName = formCollection["EmpName"];
employee.EmpDesignation =
formCollection["EmpDesignation"];
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
employeeBuisnessLayer.Employee_Insert(employee);
return RedirectToAction("Index");
}
OR
Control Name and Parameter Name must Match
[HttpPost]
public ActionResult Create(string EmpName, string EmpDesignation)
{
Employee employee = new Employee();
employee.EmpName = EmpName;
employee.EmpDesignation =
EmpDesignation;
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
employeeBuisnessLayer.Employee_Insert(employee);
return RedirectToAction("Index");
}
OR
ModelState is a collection of Name, Value pair and it's associated error message when submitted to server.
[HttpPost]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
employeeBuisnessLayer.Employee_Insert(employee);
return RedirectToAction("Index");
}
else
{
return View();
}
}
OR
UpdateModel throws an exception if it runs into binding errors.
[HttpPost]
[ActionName("Create")]
public ActionResult Create_Post()
{
Employee employee = new Employee();
UpdateModel(employee);
if (ModelState.IsValid)
{
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
employeeBuisnessLayer.Employee_Insert(employee);
return RedirectToAction("Index");
}
else
{
return View();
}
}
OR
TryUpdateModel does not throw an exception if it runs into binding errors. Instead it logs the error in the model state dictionary. which can be checked using ModelState.IsValid
[HttpPost]
[ActionName("Create")]
public ActionResult Create_Post()
{
Employee employee = new Employee();
TryUpdateModel(employee);
if (ModelState.IsValid)
{
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
employeeBuisnessLayer.Employee_Insert(employee);
return RedirectToAction("Index");
}
else
{
return View();
}
}
Step 21 : Run the Application
By making the Employee Attributes Required in BuisnessLayer Employee class, we can achieve this Model Errors
0 comments:
Post a Comment