如何使用MVC 4多选择? [英] How to use multiple select in mvc 4?

查看:82
本文介绍了如何使用MVC 4多选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用多个选择
选为
我有技能的模型一样,

I want to use multiple select in Chosen. I have Skill model like,

public class Skill 
    {
        public int Id { get; set; }
        public string Name { get; set; }        
    }

这工作在我的应用程序:

This works in my application:

    <select data-placeholder="Choose a Country..." class="chzn-select" multiple >
           <option value=""></option>
           <option value="United States">United States</option>
           <option value="Albania">Albania</option>
           <option value="Algeria">Algeria</option>
   </select>

我想用我的数据来代替国家的数据。在控制器我写:

I want to replace Countries data with my data. In controller i write:

        var list = MyService.LoadAllSkills();
        ViewBag.Skills = new MultiSelectList(list, "Id", "Name");

在视图:

@Html.ListBox("Name", ViewBag.Skills as MultiSelectList,
              new { @class = "chzn-select" } )

观结果的 @ Html.ListBox() @ Html.DropDownList()并不像&LT;选择&GT;

我得到这样结果是:

不过,我想要得到的结果。

But, I want to get result as

我怎样才能改变选择的采样?

How can I change Chosen sample?

推荐答案

我可以硬codeD样板间看到(你说,是工作),你的ListBox中的辅助生成唯一的区别和一个是缺少数据占位符属性。所以:

The only difference I can see between the hardcoded example (which you stated that is working) and the one you generate with the ListBox helper is the absence of the data-placeholder attribute. So:

@Html.ListBox(
    "Countries", 
    ViewBag.Skills as MultiSelectList,
    new { @class = "chzn-select", data_placeholder = "Choose a Country..." } 
)

这至少应该产生相同的标记,你说的是什么工作。如果不工作,那么可能是你还没有安装插件或正确,你有一些其他的JavaScript错误。阅读插件关于应该如何设置的文档。

This should at least generate the same markup as what you said is working. If it doesn't work then probably you haven't setup the plugin correctly or you have some other javascript errors. Read the documentation of the plugin about how it should be setup.

这篇关于如何使用MVC 4多选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆