Hi AbushNasi,
Please refer below sample.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<SelectListItem> items = PopulateData();
return View(items);
}
[HttpPost]
public ActionResult Index(List<SelectListItem> items)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
string query = "INSERT INTO TableName (LC, PG, PROT) VALUES (@LC, @PG, @PROT)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@LC", items[0].Value);
cmd.Parameters.AddWithValue("@PG", items[1].Value);
cmd.Parameters.AddWithValue("@PROT", items[2].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
return View(items);
}
public List<SelectListItem> PopulateData()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "LC", Value = "0" });
items.Add(new SelectListItem { Text = "PG", Value = "1" });
items.Add(new SelectListItem { Text = "PROT", Value = "2" });
return items;
}
}
View
@model List<SelectListItem>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.CheckBoxFor(m => m[i].Selected)
</td>
<td>
@Html.DisplayFor(m => m[i].Text)
@Html.HiddenFor(m => m[i].Value)
@Html.HiddenFor(m => m[i].Text)
</td>
</tr>
}
</table>
<br />
<input type="submit" value="Submit" />
}
</body>
</html>