如何在ASP.NET MVC中的DropdownList中插入默认值 [英] How to insert default value in DropdownList in ASP.NET MVC

查看:68
本文介绍了如何在ASP.NET MVC中的DropdownList中插入默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,



我对以下情况有疑问。



我的代码获取客户名单如下。我要求在客户列表中添加ALL选项并绑定到下拉列表。我的代码是

All,

I have a question on the below scenario.

My code to get customer list as below. My requirement to add 'ALL' option on the customer list and bind to the dropdown list. My code is

       public IEnumerable<SelectListItem> customerList;
       public IEnumerable<SelectListItem> CustomerList { get { return customerList; } }


private void LoadCustomerList()
       {
           customerList = db.Customers
               .Select
               (
                   c => new SelectListItem
                   {
                       Text = c.Name,
                       Value = c.Name,
                   }
               );
       }



任何人请解释我如何在结果集上添加默认选项为'ALL'。


Any one please explain me how to add the default option as 'ALL' on the result set.

推荐答案

你不需要添加结果。你可以从视图本身那样做,如下所示

You don't need of adding in the result.You can do it from view itself like below
@Html.DropDownListFor(model => model.Name, (IEnumerable<SelectListItem>)ViewData["YourCollection"], "ALL", new { @class = "select" })





第三个参数是默认值。



希望这有助于



the third Parameter is the default value.

Hope this helps


这篇关于如何在ASP.NET MVC中的DropdownList中插入默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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