如何在mvc中设置下拉列表的默认值 [英] how to set default value for dropdown in mvc

查看:139
本文介绍了如何在mvc中设置下拉列表的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在这里,我完成了如何设置默认值(布伦特詹金斯)的想法和技巧。谢谢。现在我想设置下拉国家的默认值



描述:

这里有很多国家但是我想印度是默认的country

这里我的代码: -

IN实体模型



 [必填] 

public int Country_Id {获得; set ; }







IN控制器: -



列表<   SelectListItem  >  list = new List <   SelectListItem  > (); 
list.Add(new SelectListItem {Text =Select Country,Value =Select Country}); //设置DropdownList的默认文本
var cat =(来自db.Country中的c选择c ).ToArray(); //使用linq
从数据库获取数据(int i = 0; i < cat.Length; i ++)

{

< span class =code-attribute>
list.Add(new SelectListItem { 文字 = cat [i] .Country_Name, = cat [i] .Country_Id.ToString() });

< span class =code-attribute>
}

ViewData [ViewCountry] = list;





如何在实体模型中设置默认值。你解释我的细节

解决方案

你可以参考这个



三种方式填充选定值.html [ ^ ]



希望它会有所帮助...

这里是格式



 Html.DropDownListFor(yourid,新的SelectList(下拉列表)值),选择值)





如果你已经知道Country_Id(印度),只需在选定值上硬编码


hi to all,
Here i done how to set default value as per(Brent Jenkins) ideas and tricks.thanks for that. Now i want to set default values for drop down for country

DESCRIPTION:
Here there r so many countries but i want india is default country
Here my code:-
IN Entities model

[Required]

       public int Country_Id { get; set; }




IN controller:-

List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });//set default text for DropdownList
            var cat = (from c in db.Country select c).ToArray();//get data from database using linq
            for (int i = 0; i < cat.Length; i++)

            {

                list.Add(new SelectListItem { Text = cat[i].Country_Name, Value = cat[i].Country_Id.ToString() });

            }

            ViewData["ViewCountry"] = list;



How to set default value in entities model.CAn you explain me detail

解决方案

You can refer this

three-ways-to-populate-selected-value.html[^]

Hope it will help...


here is the format

Html.DropDownListFor("yourid",new SelectList("drop down values"),"selected value")



if u already know Country_Id(india),just hard code it on "selected value"


这篇关于如何在mvc中设置下拉列表的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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