Hi acne.star52,
Please refer below sample.
Model
public class Employees
{
public string Emp_Id { get; set; }
public string Emp_Name { get; set; }
public string Emp_Code { get; set; }
public string Emp_Country { get; set; }
}
Namespaces
using System.Configuration;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<Employees> employee = new List<Employees>();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT * FROM Emp_Table";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
employee.Add(new Employees
{
Emp_Id = sdr["Emp_Id"].ToString(),
Emp_Name = sdr["Emp_Name"].ToString(),
Emp_Code = sdr["Emp_Code"].ToString(),
Emp_Country = sdr["Emp_Country"].ToString()
});
}
}
con.Close();
}
}
return View(employee);
}
[HttpPost]
public ActionResult Submit()
{
return RedirectToAction("InsertData", "Home");
}
[HttpGet]
public ActionResult InsertData()
{
return View(new Employees());
}
[HttpPost]
public ActionResult InsertData(Employees employees)
{
string query = "INSERT INTO Emp_Table VALUES(@Emp_Id, @Emp_Name, @Emp_Code, @Emp_Country)";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
cmd.Parameters.AddWithValue("@Emp_Id", employees.Emp_Id);
cmd.Parameters.AddWithValue("@Emp_Name", employees.Emp_Name);
cmd.Parameters.AddWithValue("@Emp_Code", employees.Emp_Code);
cmd.Parameters.AddWithValue("@Emp_Country", employees.Emp_Country);
cmd.ExecuteNonQuery();
con.Close();
}
}
return RedirectToAction("Index", "Home");
}
}
View
Index
@using BindData_And_ShowTo_NextPage_AndUpdate.Models;
@model List<Employees>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Submit", "Home", FormMethod.Post))
{
<table cellpadding="0" cellspacing="0">
<tr>
<th>Emp_Id</th>
<th>Emp_Name</th>
<th>Emp_Code</th>
<th>Emp_Country</th>
</tr>
@foreach (Employees employees in Model)
{
<tr>
<td>@employees.Emp_Id</td>
<td>@employees.Emp_Name</td>
<td>@employees.Emp_Code</td>
<td>@employees.Emp_Country</td>
</tr>
}
<tr>
<td colspan="4" align="center"><input type="submit" value="Submit" /></td>
</tr>
</table>
}
</body>
</html>
InsertData
@model BindData_And_ShowTo_NextPage_AndUpdate.Models.Employees
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>InsertData</title>
</head>
<body>
@using (Html.BeginForm("InsertData", "Home", FormMethod.Post))
{
<table>
<tr>
<td>Id</td>
<td>@Html.TextBoxFor(m => m.Emp_Id)</td>
</tr>
<tr>
<td>Name</td>
<td>@Html.TextBoxFor(m => m.Emp_Name)</td>
</tr>
<tr>
<td>Code</td>
<td>@Html.TextBoxFor(m => m.Emp_Code)</td>
</tr>
<tr>
<td>Country</td>
<td>@Html.TextBoxFor(m => m.Emp_Country)</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}
</body>
</html>
Screenshot