In this Article I will show you how to perform "curd" operation in MVC WebGrid.As per my previous article http://mvctpoint.blogspot.in/2015/10/how-to-bind-data-to-webgrid-in-aspnet.html that having an Action column and all row it having Edit Action.
Description:
In my previous article you have already Model,Connection,View,Controller also.When you run you application over the browser then Click in WebGrid Action Edit.It will Show you Delete,Update and Cancel.you can perform the following.
This Type of Grid show when you run your Application.
When you click on Edit then the output is :
you just right down the View Code as per my previous article.you just add only in your User or HomeController the following method.
Description:
In my previous article you have already Model,Connection,View,Controller also.When you run you application over the browser then Click in WebGrid Action Edit.It will Show you Delete,Update and Cancel.you can perform the following.
This Type of Grid show when you run your Application.
When you click on Edit then the output is :
you just right down the View Code as per my previous article.you just add only in your User or HomeController the following method.
[HttpGet]
public ActionResult InsertData()
{
return RedirectToAction("Demo");
}
[HttpPost]
public ActionResult InsertData(string FirstName, string LastName, string Email, string Password, string Mobile)
{
SqlConnection con = new SqlConnection("Pass Your Connection String");
con.Open();
SqlCommand cmd = new SqlCommand("update Regist set FirstName=' " + FirstName + "',LastName = '" + LastName + "',Pass = '" + Password + "',Mobile ='" + Mobile + "' where Email ='" + Email + "' ", con);
int i = cmd.ExecuteNonQuery();
if (i == 1)
{
return RedirectToAction("Demo");
}
else
{
con.Close();
con.Dispose();
return View();
}
}
[HttpPost]
public ActionResult DeleteData(string FirstName, string LastName, string Email, string Password, string Mobile)
{
SqlConnection con = new SqlConnection("Pass Your Connection String");
con.Open();
SqlCommand cmd = new SqlCommand("delete from Regist where Email ='" + Email + "' ", con);
int i = cmd.ExecuteNonQuery();
if (i == 1)
{
return RedirectToAction("Demo");
}
else
{
con.Close();
con.Dispose();
return View();
}
}
What do you think?
I hope you will enjoy to perform "CURD" operation in WebGrid while programming with Asp.Net MVC. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.