简体   繁体   English

使用mvc4绑定下拉列表

[英]Bind Dropdown using mvc4

I need help to bind drop-down values from models. 我需要帮助来绑定模型中的下拉值。

Model.cs 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. Model .ActionsList设置为Null。

I don't know why it shows null, though I inherit the model. 我不知道为什么它显示null,尽管我继承了模型。

I need help on how to bind the SelectList value to dropdown 我需要有关如何将SelectList值绑定到下拉列表的帮助

You need to pass a instance of BloodGroup class to the view in your action method, like below: 您需要在操作方法BloodGroup类的实例传递给视图,如下所示:

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. 使用上面的例子,它会告诉你没有错误的观点,但在你所选择的项目downdownList无法正确显示。 For showing the selected item correctly, you need to change the type of Grop property to String , like below: 为了正确显示所选项目,您需要将Grop属性的类型更改为String ,如下所示:

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 --")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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