Create operation:
We take one more ActioResult
class in EmployeeController page like below
public ActionResult Create()
{
return View();
}
Add the view for Create
method.
In Create.cshtml View
page we create some controls here
First we have two types
of controls here
1.Html
helper controls: we can write html controls and basic C# methods
2. Html
controls: only html controls
Now we take html helper
controls
Syntax for form tag:
@using
(Html.BeginForm())
{
}
Html.BeginForm used to create form tag for us.
Now we create some
controls inside the form tag(add below code in Create.cshtml page)
@using (Html.BeginForm())
{
@Html.Label("EmpName")
@Html.TextBox("EmpName")
<br />
@Html.Label("EmpSalary")
@Html.TextBox("EmpSalary")
<br />
<input type="submit" value="save" class="btn
btn-success" />
}
Now we go back employeecontroller before create method we define one attribute like [HttpGet]
Copy the below code in EmployeeController Page
[HttpGet]
public
ActionResult Create()
{
return
View();
}
[HttpPost]
public
ActionResult Create(EmployeModel emp)
{
int i =
db.saveEmployeeDetails(emp);
if (i
> 0)
{
return
RedirectToAction("index");
}
else
{
return
View();
}
}
HttpGet and HttpPost this
are called as action verbs.
HttpGet will handle get
request
HttpPost will handle
post request
Its nothing but when we
want some data or page to be loaded at the time we will go for HttpGet request only.
When we submit some data to httppost. this post will send that data to server.
Create store procedure
for save employee details
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE sp_SaveEmployeeDetails_Warriors
@EmpName varchar(50),
@EmpSalary int
AS
BEGIN
insert into
EmployeeModels(EmpName,EmpSalary)values(@EmpName, @EmpSalary)
select @@IDENTITY as int
END
GO
Copy below code in
EmployeeContext.cs file
public int
saveEmployeeDetails(EmployeModel obj)
{
SqlCommand cmd = new
SqlCommand("sp_SaveEmployeeDetails_Warriors", con);
cmd.CommandType =
CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue("@EmpName",obj.EmpName
);
cmd.Parameters.AddWithValue("@EmpSalary",
obj.EmpSalary);
object i =
cmd.ExecuteScalar();
int result
= Convert.ToInt32(i);
con.Close();
return
result;
}
No comments:
Post a Comment