Click here to Skip to main content
15,070,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

  1. @Html.LabelFor(m => m.EMP_NAME)
    @Html.TextBoxFor(m => m.EMP_NAME)
  2. @Html.LabelFor(m => m.EMP_ADDRESS)
    @Html.TextBoxFor(m => m.EMP_ADDRESS)
  3. @Html.LabelFor(m => m.EMP_MOBILE)
    @Html.TextBoxFor(m => m.EMP_MOBILE)
  4. @Html.LabelFor(m => m.EMP_EMAIL)
    @Html.TextBoxFor(m => m.EMP_EMAIL)
  5. @* 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" />


}
Posted
Updated 20-Mar-14 1:46am
v2

1 solution

See if coloumn name is Statename or not

Controllers -> ActionResult Index() 
         //Get list of Countries to load on dropdownlist
            ViewBag.State= new SelectList(user.GetAllState(), "ID", "StateName", "ID");

        Views -> Index.cshtml
          @Html.DropDownList("State", ViewBag.State as SelectList, 
          "Select State", new { style = "width:200px" })
   
Comments
Bhanu Pratap Verma 20-Mar-14 8:28am
   
Hey sankarshan, now its working fine. thanks a lots.
[no name] 20-Mar-14 8:38am
   
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900