简体   繁体   中英

Bind Dropdown using mvc4

I need help to bind drop-down values from models.

Model.cs

public class BloodGroup
{
    public BloodGroup()
    {
        ActionsList = new List<SelectListItem>();
    }
    [Display(Name="Blood Group")]
    public int Group { get; set; }
    public IEnumerable<SelectListItem> ActionsList { get; set; }     
}

public class ActionType
 {
     public int GroupId { get; set; }
     public string BloodGroup { get; set; } 
 }

In the Controller:

List<ActionType> actionType = GetCourses();
bGroup.ActionsList = from action in actionType
                             select new SelectListItem
                             {
                                 Text = action.BloodGroup,
                                 Value = ((int)action.GroupId).ToString(),
                                 Selected = action.BloodGroup.Equals("A+")?true:false
                             };
return view;

public List<ActionType> GetCourses()
{
        return new List<ActionType> { 
                    new ActionType () {  GroupId = 1, BloodGroup = "A+"}, 
                    new ActionType () {  GroupId = 2, BloodGroup = "B+"},
                    new ActionType () {  GroupId = 3, BloodGroup = "O+" },
                    new ActionType () {  GroupId = 4, BloodGroup = "AB+" },
                    new ActionType () {  GroupId = 5, BloodGroup = "A-"}, 
                    new ActionType () {  GroupId = 6, BloodGroup = "B-"},
                    new ActionType () {  GroupId = 7, BloodGroup = "O-" },
                    new ActionType () {  GroupId = 8, BloodGroup = "AB-" }
             };
  }

It successfully return to view. But in view when bind dropdown it throws an error.

in view

@model MyMVC.Models.BloodGroup

@Html.DropDownListFor(m => m.Group, new SelectList(Model.ActionsList, "Value", "Text",true), "-- Select --")</li>

It returns error.

Object reference not set to an instance of an object.

Model .ActionsList is set a Null.

I don't know why it shows null, though I inherit the model.

I need help on how to bind the SelectList value to dropdown

You need to pass a instance of BloodGroup class to the view in your action method, like below:

public ActionResult YourAction()
{
        List<ActionType> actionType = GetCourses();
        var model = new BloodGroup()
        {
            ActionsList = (from action in actionType
                select new SelectListItem
                {
                    Text = action.BloodGroup,
                    Value = ((int) action.GroupId).ToString(),
                    Selected = action.BloodGroup.Equals("A+")
                })
        };
        return View(model);
}

Then in your view:

@model  BloodGroup    
@Html.DropDownListFor(m => m.Group, Model.ActionsList,"-- Select --")

Notice

Using above example it'll show you the view without errors, but the selected item in your downdownList will NOT show correctly. For showing the selected item correctly, you need to change the type of Grop property to String , like below:

public class BloodGroup
{
     //
    [Display(Name = "Blood Group")]
    public string Group { get; set; } 
    //
}

Then use above same action method, make your view like:

@model  BloodGroup
@Html.DropDownList("Group", Model.ActionsList, "-- Select --")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM