Hi All,
I am very new in MVC 4.0, I am doing practice own self over new technology. I am stuck at binding dropdown list for state and after that get district on bases of state.
I am posting my model, view and controller code.
provide solution is highly appreciated.
MODEL:
public class EmployeeRegitrationModel
{
[Required(ErrorMessage = "Please Enter the Name")]
[Display(Name = "Employee Name")]
public string EMP_NAME { get; set; }
[Required(ErrorMessage = "Please Enter the full Address ")]
[Display(Name = "Employee Address")]
public string EMP_ADDRESS { get; set; }
[Required(ErrorMessage = "Please Enter Mobile Number")]
[Display(Name = "Employee Mobile")]
[DataType(DataType.PhoneNumber)]
public string EMP_MOBILE { get; set; }
[Required]
[Display(Name = "Employee Email")]
[DataType(DataType.EmailAddress)]
public string EMP_EMAIL { get; set; }
public string STATE_CODE { get; set; }
public string STATE_NAME { get; set; }
public int EmployeeRegitration(EmployeeRegitrationModel user)
{
SqlConnection con = new SqlConnection("timeout=200; Server=lsipl_dc; Database=TestMVC;User Id=sa;Password=Lsipl#2013");
SqlCommand cmd = new SqlCommand("PROC_EMPREGISTRATION", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@QTYPE", 1);
cmd.Parameters.Add("@EMP_NAME", user.EMP_NAME);
cmd.Parameters.Add("@EMP_ADDRESS", user.EMP_ADDRESS);
cmd.Parameters.Add("@EMP_MOBILE", user.EMP_MOBILE);
cmd.Parameters.Add("@EMP_EMAIL", user.EMP_EMAIL);
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
public List<employeeregitrationmodel> GetAllState()
{
List<employeeregitrationmodel> GetState = new List<employeeregitrationmodel>();
SqlConnection con = new SqlConnection("timeout=200; Server=lsipl_dc; Database=TestMVC;User Id=sa;Password=Lsipl#2013");
con.Open();
SqlCommand cmd = new SqlCommand("GETALLSTATE", con);
SqlDataReader dr;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@QTYPE", 1);
dr = cmd.ExecuteReader();
if (dr.Read())
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
user.STATE_CODE = dr["CODE"].ToString();
user.STATE_NAME = dr["NAME"].ToString();
GetState.Add(user);
}
return GetState;
}
public List<employeeregitrationmodel> GetEmployeeDetails()
{
List<employeeregitrationmodel> GetRecodrs = new List<employeeregitrationmodel>();
SqlConnection con = new SqlConnection("timeout=200; Server=lsipl_dc; Database=TestMVC;User Id=sa;Password=Lsipl#2013");
con.Open();
SqlCommand cmd = new SqlCommand("PROC_EMPSHOWALL", con);
SqlDataReader dr;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@QTYPE", 1);
dr = cmd.ExecuteReader();
while (dr.Read())
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
user.EMP_NAME = dr["EMP_NAME"].ToString();
user.EMP_ADDRESS = dr["EMP_ADDRESS"].ToString();
user.EMP_MOBILE = dr["EMP_MOBILE"].ToString();
user.EMP_EMAIL = dr["EMP_EMAIL"].ToString();
GetRecodrs.Add(user);
}
con.Close();
return GetRecodrs;
}
}
======Controller:
public class EmployeeRegitrationController : Controller
{
//
// GET: /EmployeeRegitration/
[HttpGet]
public ActionResult Index()
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
List<employeeregitrationmodel> strlist = new List<employeeregitrationmodel>();
strlist = user.GetAllState();
return View(strlist);
}
[HttpPost]
public ActionResult Index(EmployeeRegitrationModel user)
{
user.EmployeeRegitration(user);
return View();
}
[HttpGet]
public ActionResult GetAllEmpDetails()
{
EmployeeRegitrationModel emp = new EmployeeRegitrationModel();
List<employeeregitrationmodel> strlist = new List<employeeregitrationmodel>();
strlist = emp.GetEmployeeDetails();
return View(strlist);
}
}
==========================View
@model MyFirst_SimpleMVCProject.Models.EmployeeRegitrationModel
@{
ViewBag.Title = "Employee Registration";
}
<hgroup class="title">
@ViewBag.Title.
@*
Create a new account.
*@
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
Employee Registration Form
- @Html.LabelFor(m => m.EMP_NAME)
@Html.TextBoxFor(m => m.EMP_NAME)
- @Html.LabelFor(m => m.EMP_ADDRESS)
@Html.TextBoxFor(m => m.EMP_ADDRESS)
- @Html.LabelFor(m => m.EMP_MOBILE)
@Html.TextBoxFor(m => m.EMP_MOBILE)
- @Html.LabelFor(m => m.EMP_EMAIL)
@Html.TextBoxFor(m => m.EMP_EMAIL)
- @* Here I want to get the dropdown list and bind with state master *@
@*@Html.LabelFor(m => m.STATE_NAME)*@
@* @Html.DropDownList("title")*@
<input type="submit" value="Register" />
}