本文介绍了如何在MVC4 Razor中使用CheckBoxList和DropdownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我必须使用@ Html.CheckBoxListFor<>或@ Html.DropdownListFor<>.我对如何在View中使用这些帮助器类感到困惑,而我正在使用列表进行模型绑定.

I have to use @Html.CheckBoxListFor<> or @Html.DropdownListFor<>.I am confused about how to use these helper class in View whild I am using List for Model Binding.

将列表绑定到CheckBoxList和DropdownList的最简便方法是什么?

What is the Right and Easiest way to Bind List into CheckBoxList and DropdownList.

在模型中:

public class SampleViewModel
    {
        public IEnumerable<Responsible> AvailableResponsibles { get; set; }
        public IEnumerable<Responsible> SelectedResponsibles { get; set; }
        public PostedResponsibles PostedResponsibles { get; set; }
        Responsible objResponsible = new Responsible();
        public SampleViewModel GetResponsiblesInitialModel()
        {
            //setup properties
            var model = new SampleViewModel();
            var selectedResponsibles = new List<Responsible>();

            //setup a view model
            model.AvailableResponsibles = objResponsible.GetAll().ToList();
            model.SelectedResponsibles = selectedResponsibles;
            return model;
        }
    }
public class Responsible
    {
        //Integer value of a checkbox
        public int Id { get; set; }

        //String name of a checkbox
        public string Name { get; set; }

        //Boolean value to select a checkbox
        //on the list
        public bool IsSelected { get; set; }

        //Object of html tags to be applied
        //to checkbox, e.g.:'new{tagName = "tagValue"}'
        public object Tags { get; set; }

        public IEnumerable<Responsible> GetAll()
        {
            return new List<Responsible> {
                              new Responsible {Name = "Apple", Id = 1 },
                              new Responsible {Name = "Banana", Id = 2},
                              new Responsible {Name = "Cherry", Id = 3},
                              new Responsible {Name = "Pineapple", Id = 4},
                              new Responsible {Name = "Grape", Id = 5},
                              new Responsible {Name = "Guava", Id = 6},
                              new Responsible {Name = "Mango", Id = 7}
                            };
        }

    }

    public class PostedResponsibles
    {
        //this array will be used to POST values from the form to the controller
        public string[] ResponsibleIds { get; set; }
    }

视图是:

<tr>
                <td>
                    @Html.LabelFor(model=>model.Responsible)
                </td>
                <td>
                  @Html.CheckBoxListFor(model => model.PostedResponsibles.ResponsibleIds,
                                  model => model.AvailableResponsibles,
                                  Responsible => Responsible.Id,
                                  Responsible => Responsible.Name,
                                  model => model.SelectedResponsibles)

                </td>
            </tr>

目前我使用的是Nuget Packege,但有点令人困惑且难以使用.建议我其他人来实现这一目标.

Currently I am using Nuget Packege but it is little bit confusing and hard to use.Suggest me any other to achieve this one.

推荐答案

这取决于您的UI要求.如果您需要一个下拉列表,请使用该列表.

It depend on your UI requirement. if you need a drop down list then use that.

CheckBoxList不是开箱即用的帮助器,它是一个自定义帮助器.

CheckBoxList is not out of the box helper, it is a custom helper.

您可以从此处 代码项目

You can find more details of the helper (CheckBoxList) from here Code Project

关于下拉菜单,请参考 MSDN .可以通过多种方式重载它.

Regarding Drop down please refer to MSDN. It can be overloaded in multiple ways.

以下是可用的 ASP.NET MVC帮助器

Here are the available ASP.NET MVC helpers

这篇关于如何在MVC4 Razor中使用CheckBoxList和DropdownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:23