jitendra

jitendra

  • 1.3k
  • 430
  • 67.6k

Dropdown List Binding , is that Right or Rough in MVC

Sep 6 2013 1:56 PM
Code - Razor Code
  <div class="editor-field">
  @Html.DropDownList("CountryID", String.Empty)
  @Html.ValidationMessageFor(model => model.CountryID)
  </div>

Controller code
at the time on showing data in drop down list
 ViewBag.CountryID = new SelectList(db.Countries, "CountryID", "Name");

at the time of showing data on saving time
ViewBag.CountryID = new SelectList(db.Countries, "CountryID", "Name", state.CountryID);

plz send your comment - how many types we have bind data in drop downlist

Controller

Code

using Mvc3App.Models.Entity;
using Mvc3App.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Mvc3App.Controllers
{
  public class AccountController : Controller
  {
  WebSolutionDBEntities db = new WebSolutionDBEntities();
  //
  // GET: /Account/

  #region ............ Branch ---------------------


  [AcceptVerbs(HttpVerbs.Get)]
  public JsonResult GetDivisionByBranch(string BranchID)
  {
  DivisionModel objState = new DivisionModel();
  var modelList = objState.GetDivisionByBranch(Convert.ToInt32(BranchID));

  var modelData = modelList.Select(m => new SelectListItem()
  {
  Text = m.Division_Name,
  Value = m.Division_ID.ToString(),

  });

  return Json(modelData, JsonRequestBehavior.AllowGet);
  }

  [AcceptVerbs(HttpVerbs.Get)]
  public JsonResult GetSubDivisionByDivisionID(string DivisionID)
  {
  SubDivisionModel objCity = new SubDivisionModel();
  var colourList = objCity.GetSubDivisionByDivisionID(Convert.ToInt32(DivisionID));

  var colourData = colourList.Select(c => new SelectListItem()
  {
  Text = c.Sub_Div_Name,
  Value = c.Sub_Div_ID.ToString(),

  });

  return Json(colourData, JsonRequestBehavior.AllowGet);
  }


  #endregion



  #region ---- Methods for cascading dropdown list
  //private IList<State> GetModels(int id)
  //{
  //  return db.States.Where(m => m.CountryID == id).ToList();
  //}

  //private IList<State> GetStates(int CountryID)
  //{
  //  return
 
  //}

  //private IList<City> GetColours(int id)
  //{
  //  return db.Cities.Where(c => c.SatateID == id).ToList();
  //}


  [AcceptVerbs(HttpVerbs.Get)]
  public JsonResult LoadModelsByCar(string CountryID)
  {
  StateModel objState = new StateModel();
  var modelList = objState.GetStates(Convert.ToInt32(CountryID));

  var modelData = modelList.Select(m => new SelectListItem()
  {
  Text = m.Name,
  Value = m.CountryID.ToString(),

  });

  return Json(modelData, JsonRequestBehavior.AllowGet);
  }

  [AcceptVerbs(HttpVerbs.Get)]
  public JsonResult LoadColoursByModel(string StateID)
  {
  CityModel objCity = new CityModel();
  var colourList = objCity.GetCityByStateID(Convert.ToInt32(StateID));

  var colourData = colourList.Select(c => new SelectListItem()
  {
  Text = c.Name,
  Value = c.SatateID.ToString(),

  });

  return Json(colourData, JsonRequestBehavior.AllowGet);
  }



  #endregion ----------




  public ActionResult Index()
  {
  CountryModel objCountry = new CountryModel();

  ViewBag.Cars = db.Countries.ToList();
  ViewBag.Models = db.States.ToList();
  ViewBag.Colours = db.Cities.ToList();
  return View("SignUp");
  }


  public ActionResult Register()
  {
  //  ViewBag.Country = db.Countries.ToList();
  //  ViewBag.State = db.States.ToList();
  //  ViewBag.City = db.Cities.ToList();

 

  CountryModel objCountry = new CountryModel();


  ViewBag.Country = objCountry.GetCountryItemAll();
  ViewBag.State = db.States.ToList();
  ViewBag.City = db.Cities.ToList();
  return View("SignUp");
  }

  [HttpPost]
  public ActionResult Register(RegistrationModel objRegister)
  {

  // ViewBag.Country = db.Countries.ToList();
  // ViewBag.State = db.States.ToList();
  // ViewBag.City = db.Cities.ToList();
  CountryModel objCountry = new CountryModel();
  ViewBag.Country = objCountry.GetCountryItemAll();
  ViewBag.State = db.States.ToList();
  ViewBag.City = db.Cities.ToList();
  if (objRegister.User_Insert_Buss(objRegister) != 0)
  {
  ViewBag.Message = "Successful";
 
  }
  return View("SignUp", objRegister);
 
  }
 
  public ActionResult Login()
  {
 
  ViewBag.Cars = db.Countries.ToList();
  ViewBag.Models = db.States.ToList();
  ViewBag.Colours = db.Cities.ToList();
 
  return View("SignIn");

  }

  [HttpPost]
  public ActionResult Login(RegistrationModel objRegister)
  {
  WebSolutionDBEntities db = new WebSolutionDBEntities();
  State state = new State();

  if (objRegister.Login(objRegister) != null)
  {
  ViewBag.Message = "Successfull";

  return RedirectToAction("MyAccount");
  }
  return View("SignIn");

  }
  public ActionResult MyAccount()
  {
 
  return View("MyAccount");

  }

  }
}


Model classes
using Mvc3App.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Mvc3App.Models.Entity
{
  public class RegistrationModel
  {
  WebSolutionDBEntities context = new WebSolutionDBEntities();
  public int UserID { get; set; }
  public string EamilAddress { get; set; }
  public string Password { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public CountryModel CountryModels { get; set; }
  public StateModel StateModels { get; set; }
  public CityModel CityModels { get; set; }
  public int UserRoleID { get; set; }
  public int RoleID { get; set; }
  public bool IsActive { get; set; }
  public string IPAddress { get; set; }


  #region ------- Country---State---City--------

 

  #endregion




  #region .... Methods

  public int User_Insert_Buss(RegistrationModel objRegister)
  {
  int a = 0; 
  UserProfile objprofile = new UserProfile();
  objprofile.CountryID = 1;
  objprofile.CityID = objRegister.StateModels.SatateID;
  objprofile.SatateID = objRegister.CityModels.CityID;
  objprofile.FirstName = objRegister.CountryModels.CountryName;
  objprofile.LastName = objRegister.LastName;
  objprofile.Email = objRegister.EamilAddress;
  objprofile.Password = objRegister.Password;
  objprofile.DOB = DateTime.Now;
  objprofile.CreatedDate = DateTime.Now;

  context.UserProfiles.Add(objprofile);
  context.SaveChanges();

  int a1 = objprofile.UserId;

  if (a1 != 0)
  {
  //UserRole_Mapping objrole = new UserRole_Mapping();
  //objrole.UserID = a1;
  //objrole.RoleID = 1;
  //context.UserRole_Mapping.Add(objrole);
  //context.SaveChanges();
 
 
  }


  return a;
  }

  public RegistrationModel Login(RegistrationModel objlogin)
  {
 
  var user = context.UserProfiles.FirstOrDefault(u => u.Email == objlogin.EamilAddress);
  if (user != null)
  {
  return objlogin;
  } 
  return objlogin;
  }

  #endregion

  }
}

Razor source

@model Mvc3App.Models.Entity.RegistrationModel

<script type="text/javascript">
  $(document).ready(function ()
  {
  $("#ddlCars").change(function ()
  {
  var idModel = $(this).val();
  $.getJSON("/Index/LoadModelsByCar", { id: idModel },
  function (carData) {
  var select = $("#ddlModels");
  select.empty();
  select.append($('<option/>', {
  value: 0,
  text: "Select a Model"
  }));
  $.each(carData, function (index, itemData) {

  select.append($('<option/>', {
  value: itemData.Value,
  text: itemData.Text
  }));
  });
  });
  });
  $("#ddlModels").change(function () {
  var idColour = $(this).val();
  $.getJSON("/Index/LoadColoursByModel", { id: idColour },
  function (modelData) {
  var select = $("#ddlColours");
  select.empty();
  select.append($('<option/>', {
  value: 0,
  text: "Select a Colour"
  }));
  $.each(modelData, function (index, itemData) {

  select.append($('<option/>', {
  value: itemData.Value,
  text: itemData.Text
  }));
  });
  });
  });
  });

</script>

@using (Html.BeginForm("Register","Account"))
{
 
  <table style="width:100%">
  <tr>
  <td style="width:10%">

  Your username
  </td>
 
  <td style="width:90%">

  @Html.TextBoxFor(m => m.EamilAddress, new {  placeholder="mysuperusername690",required="required" })
  </td>

  </tr>
  <tr>
  <td style="width:10%">
  Country
  </td>
  <td style="width:90%">
  @Html.DropDownListFor(Model => Model.CountryModels.CountryName, new SelectList(ViewBag.Country as System.Collections.IEnumerable, "CountryID", "Name"),
  "Select a Car", new { id = "ddlCars" })
  </td>

  </tr>
  <tr>
  <td style="width:10%">
State
  </td>
  <td style="width:90%">
  @Html.DropDownListFor(Model => Model.StateModels.SatateID, new SelectList(Enumerable.Empty<SelectListItem>(), "StateID", "Name"),
  "Select a Model", new { id = "ddlModels" })
  </td>

  </tr>
  <tr>
  <td style="width:10%">
  City

  </td>
  <td style="width:90%">
  @Html.DropDownListFor(Model => Model.CityModels.CityID, new SelectList(Enumerable.Empty<SelectListItem>(), "CityID", "Name"),
  "Select a Colour", new { id = "ddlColours" })
  </td>

  </tr>
  <tr>
  <td style="width:10%">
  Your email
  </td>
  <td style="width:90%">
  @Html.TextBoxFor(m => m.EamilAddress, new { placeholder="[email protected]",required="required" })
  </td>

  </tr>
  <tr>
  <td style="width:10%">
  Password
  </td>
  <td style="width:90%">
  @Html.PasswordFor(m => m.Password, new { placeholder="eg. X8df!90EO",required="required" })
  </td>

  </tr>

  <tr>
  <td style="width:10%">
  Confirm Password
  </td>
  <td style="width:90%">
  @Html.PasswordFor(m => m.Password, new { placeholder="eg. X8df!90EO",required="required" })
  </td>

  </tr>
  <tr>
  <td style="width:10%">
  Confirm Password
  </td> <td style="width:90%">  <input type="submit" value="Sign up"/> </td>
 

  </tr>


  </table>
 
}


Answers (2)