Hi sunnyk21,
You are selecting single Designation. So change the with string or int value instead of List<SelectListItem>.
In Index post method reassign the DesignationList and return the Employee model.
I have modified the Model and Controller code. Refer below code.
Model
public class Employee
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Gender { get; set; }
[Required]
public string Designation { get; set; }
public List<SelectListItem> DesignationList { get; set; }
}
Namespaces
using System.Configuration;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
string st = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public List<SelectListItem> GetDesignation()
{
List<SelectListItem> desiglist = new List<SelectListItem>();
using (SqlConnection con = new SqlConnection(st))
{
string query = "SELECT * FROM Designation";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
desiglist.Add(new SelectListItem
{
Text = rd["Designation"].ToString(),
Value = rd["DesigId"].ToString()
});
}
con.Close();
}
}
return desiglist;
}
public ActionResult Index()
{
Employee emp = new Employee();
emp.DesignationList = GetDesignation();
return View(emp);
}
[HttpPost]
public ActionResult Index(Employee emp)
{
emp.DesignationList = GetDesignation();
if (ModelState.IsValid)
{
using (SqlConnection con = new SqlConnection(st))
{
string query = "insert into EmployeeDetail(FirstName,LastName,Gender,Designation) values(@FirstName,@LastName,@Gender,@Designation)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@FirstName", emp.FirstName);
cmd.Parameters.AddWithValue("@LastName", emp.LastName);
cmd.Parameters.AddWithValue("@Gender", emp.Gender);
cmd.Parameters.AddWithValue("@Designation", emp.Designation);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
return View(emp);
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Save_From_DropDownList_MVC.Models.Employee>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<% using (Html.BeginForm("Index", "Home", FormMethod.Post))
{%>
<%:Html.AntiForgeryToken()%>
<div class="form-horizontal">
<h4>
Employee</h4>
<hr />
<%:Html.ValidationSummary(true, "", new { @class = "text-danger" })%>
<div class="form-group">
<%:Html.LabelFor(model => model.FirstName)%>
<div class="col-md-10">
<%:Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })%>
<%:Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })%>
</div>
</div>
<div class="form-group">
<%:Html.LabelFor(model => model.LastName)%>
<div class="col-md-10">
<%:Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })%>
<%:Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })%>
</div>
</div>
<div class="form-group">
<%:Html.LabelFor(model => model.Gender)%>
<div class="col-md-10">
<%:Html.DropDownListFor(model=>model.Gender,new List<SelectListItem>
{
new SelectListItem{Text="Male" , Value="1"},
new SelectListItem{Text="Female" , Value="2"}
})%>
</div>
</div>
<div class="form-group">
<%:Html.LabelFor(model => model.Designation)%>
<div class="col-md-10">
<%:Html.DropDownListFor(model=>model.Designation,Model.DesignationList,"Please Select Designation")%>
</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>
</body>
</html>
Screenshot
