Dropdown list - MVC3

2019-06-06 19:58发布

问题:

I am using C# in ASP MVC3. I have two tables from SQL Server.Table names are SMS_User and SMS_Division in SQL Server 2008. When i create a new user, I want to show division id from sms_division table.

SMS_User contains UserName, DivisionID, EmailAddress

SMS_Division contains DivisionID, DivisionName.

Controller Code :

UserController : Controller
{
   private NetPerfMonEntities2 db = new NetPerfMonEntities2(); 
   IEnumerableZamZam= db.SMS_Division.Select(c => new SelectListItem { Value = c.divisionid.ToString(), Text = c.divisionid.ToString() } );
}

When I create a new user in User Create() VIEW I want to show a DivisonName as a dropdown list instead of a text box. How I do that ?

@Html.DropDownListFor(model => model.divisionid, (IEnumerable<SelectListItem>) ViewData["Divisions"], "<--Select a divison-->") 
@Html.ValidationMessageFor(model => model.divisionid) 

I have this error message :

CS0103: The name 'sms_amountlimit2' does not exist in the current context

回答1:

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 = "testAddress@test.com" };

      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.



回答2:

In your model add a collection of the divisions then create the dropdown list like below:

@Html.DropDownListFor(m => m.SelectedDivisionId, 
                      new SelectList(Model.Divisions, "DivisionId", "DivisionName"), 
                      "-- Select Division --")