Skip to main content
corrected answer with new data from answer by OP
Source Link
Matthieu
  • 4.6k
  • 4
  • 43
  • 62

I'll be assuming a few missing part of your question in my answer, and give you a generic pattern to have a working dropdown list in ASP.NET MVC 3 :

Let's start with the models :

UserModel would be the class representing the data extracted from sms_amountlimit2sms_user

public class UserModel
{
   public string Username { get; set; }
   public string AddressEmailAddress { get; set; }
   public int DivisionId { get; set; }
}

DivisionModel would be the class representing the data extracted from sms_division

public class DivisionModel
{
   public int DivisionId { get; set; }
   public string DivisionName { get; set; }
}

By Extracted, I mean anything that can transform the data in your Database in instanciated classes. That can be an ORM (EntityFramework or others), or SQL Queries, etc...

Next, is the viewmodel, because it wouldn't make sense to plug an IEnumerable of divisions in UserModel, and I personally don't really like using ViewData when I can avoid it :

public class UserViewModel
{
   public UserModel User { get; set; }
   public IEnumerable<DivisionModel> Divisions {get; set;}
}

Next, the controller :

public class UserController : Controller
{
   public ActionResult IndexCreate()
   {
      List<DivisionModel> divisions = new List<DivisionModel>();
      divisions.Add(new DivisionModel() { DivisionId = 1, DivisionName = "Division1" });
      divisions.Add(new DivisionModel() { DivisionId = 2, DivisionName = "Division2" });

      UserModel user = new UserModel() { Username = "testUser", AddressEmailAddress = "testAddress""[email protected]" };

      return View(new UserViewModel() { User = user, Divisions = divisions });
   }
}

I just create the Division list and the user, but you would get then from you database by any means you are using.

And finally the View :

@model ViewModels.UserViewModel
@{
    ViewBag.Title = "Test";"Create";
}

<h2>Test<<h2>Create</h2>
<p>
@Html.DropDownListFor(model => model.User.DivisionId, new SelectList(Model.Divisions, "DivisionId", "DivisionName"),  "-- Select Division --") 
@Html.ValidationMessageFor(model => model.User.DivisionId)  
</p>

Note that the model binded to the view is the ViewModel.

I'll be assuming a few missing part of your question in my answer, and give you a generic pattern to have a working dropdown list in ASP.NET MVC 3 :

Let's start with the models :

UserModel would be the class representing the data extracted from sms_amountlimit2

public class UserModel
{
   public string Username { get; set; }
   public string Address { get; set; }
   public int DivisionId { get; set; }
}

DivisionModel would be the class representing the data extracted from sms_division

public class DivisionModel
{
   public int DivisionId { get; set; }
   public string DivisionName { get; set; }
}

By Extracted, I mean anything that can transform the data in your Database in instanciated classes. That can be an ORM (EntityFramework or others), or SQL Queries, etc...

Next, is the viewmodel, because it wouldn't make sense to plug an IEnumerable of divisions in UserModel, and I personally don't really like using ViewData when I can avoid it :

public class UserViewModel
{
   public UserModel User { get; set; }
   public IEnumerable<DivisionModel> Divisions {get; set;}
}

Next, the controller :

public class UserController : Controller
{
   public ActionResult Index()
   {
      List<DivisionModel> divisions = new List<DivisionModel>();
      divisions.Add(new DivisionModel() { DivisionId = 1, DivisionName = "Division1" });
      divisions.Add(new DivisionModel() { DivisionId = 2, DivisionName = "Division2" });

      UserModel user = new UserModel() { Username = "testUser", Address = "testAddress" };

      return View(new UserViewModel() { User = user, Divisions = divisions });
   }
}

I just create the Division list and the user, but you would get then from you database by any means you are using.

And finally the View :

@model ViewModels.UserViewModel
@{
    ViewBag.Title = "Test";
}

<h2>Test</h2>
<p>
@Html.DropDownListFor(model => model.User.DivisionId, new SelectList(Model.Divisions, "DivisionId", "DivisionName"),  "-- Select Division --") 
@Html.ValidationMessageFor(model => model.User.DivisionId)  
</p>

Note that the model binded to the view is the ViewModel.

I'll be assuming a few missing part of your question in my answer, and give you a generic pattern to have a working dropdown list in ASP.NET MVC 3 :

Let's start with the models :

UserModel would be the class representing the data extracted from sms_user

public class UserModel
{
   public string Username { get; set; }
   public string EmailAddress { get; set; }
   public int DivisionId { get; set; }
}

DivisionModel would be the class representing the data extracted from sms_division

public class DivisionModel
{
   public int DivisionId { get; set; }
   public string DivisionName { get; set; }
}

By Extracted, I mean anything that can transform the data in your Database in instanciated classes. That can be an ORM (EntityFramework or others), or SQL Queries, etc...

Next, is the viewmodel, because it wouldn't make sense to plug an IEnumerable of divisions in UserModel, and I personally don't really like using ViewData when I can avoid it :

public class UserViewModel
{
   public UserModel User { get; set; }
   public IEnumerable<DivisionModel> Divisions {get; set;}
}

Next, the controller :

public class UserController : Controller
{
   public ActionResult Create()
   {
      List<DivisionModel> divisions = new List<DivisionModel>();
      divisions.Add(new DivisionModel() { DivisionId = 1, DivisionName = "Division1" });
      divisions.Add(new DivisionModel() { DivisionId = 2, DivisionName = "Division2" });

      UserModel user = new UserModel() { Username = "testUser", EmailAddress = "[email protected]" };

      return View(new UserViewModel() { User = user, Divisions = divisions });
   }
}

I just create the Division list and the user, but you would get then from you database by any means you are using.

And finally the View :

@model ViewModels.UserViewModel
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>
<p>
@Html.DropDownListFor(model => model.User.DivisionId, new SelectList(Model.Divisions, "DivisionId", "DivisionName"),  "-- Select Division --") 
@Html.ValidationMessageFor(model => model.User.DivisionId)  
</p>

Note that the model binded to the view is the ViewModel.

Source Link
Matthieu
  • 4.6k
  • 4
  • 43
  • 62

I'll be assuming a few missing part of your question in my answer, and give you a generic pattern to have a working dropdown list in ASP.NET MVC 3 :

Let's start with the models :

UserModel would be the class representing the data extracted from sms_amountlimit2

public class UserModel
{
   public string Username { get; set; }
   public string Address { get; set; }
   public int DivisionId { get; set; }
}

DivisionModel would be the class representing the data extracted from sms_division

public class DivisionModel
{
   public int DivisionId { get; set; }
   public string DivisionName { get; set; }
}

By Extracted, I mean anything that can transform the data in your Database in instanciated classes. That can be an ORM (EntityFramework or others), or SQL Queries, etc...

Next, is the viewmodel, because it wouldn't make sense to plug an IEnumerable of divisions in UserModel, and I personally don't really like using ViewData when I can avoid it :

public class UserViewModel
{
   public UserModel User { get; set; }
   public IEnumerable<DivisionModel> Divisions {get; set;}
}

Next, the controller :

public class UserController : Controller
{
   public ActionResult Index()
   {
      List<DivisionModel> divisions = new List<DivisionModel>();
      divisions.Add(new DivisionModel() { DivisionId = 1, DivisionName = "Division1" });
      divisions.Add(new DivisionModel() { DivisionId = 2, DivisionName = "Division2" });

      UserModel user = new UserModel() { Username = "testUser", Address = "testAddress" };

      return View(new UserViewModel() { User = user, Divisions = divisions });
   }
}

I just create the Division list and the user, but you would get then from you database by any means you are using.

And finally the View :

@model ViewModels.UserViewModel
@{
    ViewBag.Title = "Test";
}

<h2>Test</h2>
<p>
@Html.DropDownListFor(model => model.User.DivisionId, new SelectList(Model.Divisions, "DivisionId", "DivisionName"),  "-- Select Division --") 
@Html.ValidationMessageFor(model => model.User.DivisionId)  
</p>

Note that the model binded to the view is the ViewModel.