如何在下拉列表中显示印度 [英] how to show the india in top of the dropdown

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

问题描述

代码如下:



代码

----

var usrModel = new UserRegisterModel ();

usrModel.Countries = snwe.Countries..AsEnumerable()。选择(C => new SelectListItem

{

Text = C.Country1,

Value = C.CountryID.ToString()

});



view .cshtml

------------

  • The code is given below

    code
    ----
    var usrModel = new UserRegisterModel();
    usrModel.Countries = snwe.Countries..AsEnumerable().Select(C => new SelectListItem
    {
    Text = C.Country1,
    Value = C.CountryID.ToString()
    });

    view.cshtml
    ------------



  • < label> @(LocalizationProvider.Current.CountryLabel) *

    @ Html.ValidationMessageFor (m => m.CountryId)< / label>


    <label>@(LocalizationProvider.Current.CountryLabel)*
    @Html.ValidationMessageFor(m => m.CountryId)</label>


    @ Html.DropDownListFor (m => m.CountryId,Model.Countries,Select Country,new {@class =dropdown})


    @Html.DropDownListFor(m => m.CountryId, Model.Countries, "Select Country", new { @class = "dropdown" })






  • 在上面的代码中,国家列出的代码但是我需要在下拉列表中输入第一个值是india。


    In the above code the code the countries are listing but i need to in dropdown first value is "india".

    推荐答案

    您可以创建一个单独的方法来绑定国家/地区下拉菜单 -



    You can create a separate method to bind country dropdown-

    public static SelectList GetCountries(int selected = 0)
           {
               return new SelectList(Repository.Instance.All<country>(), "Id", "Name", selected);
           }</country>




    @Html.DropDownListFor(x=>x.CountryId,GetCountries(x.CountryId),"[Select]")





    方法返回类型是 SelectList 所以你只需要在<$ c $中传递印度的 Id c> GetCountries 方法作为参数。



    另一种方法可以将所有项目添加到除印度之外的列表中,然后将印度插入位置0或将India添加为数据库表的第一行,或在数据库表和用户 OrderBy 子句中创建列 int_Order



    The method return type is SelectList so you just need to pass the Id of India in GetCountries method as parameter.

    another way you can add all the items into list except India and then insert India at position 0 or add India as first row in your database table, or create a column int_Order in your database table and user OrderBy clause.


    1.首先,你应该在你的选择中使用WHERE条件过滤掉印度(在印度以外的所有国家);



    2.然后,通过使用类似的选择(但是这个你的WHERE子句中的时间只选择印度),你应该插入(添加在第一个位置)印度作为你的 usrModel.Countries 中的第一项;



    3.以下是代码的样子:

    1.First you should filter out "India" from your selection by using WHERE condition in your fist select (all countries except India);

    2.Then, by using a similar select (but this time in your WHERE clause select only India), you should insert (add in the first position) "India" as first item in your "usrModel.Countries";

    3.Here is how the code should look like:
    List<Country> tempList = snwe.Countries.Where(c=>c.CountryShort != "IN").ToList();
    Country indiaEntry = snwe.Countries.Where(c => c.CountryShort == "IN").FirstOrDefault();
    //
    if(indiaEntry  != null)
       tempList.Insert(0, indiaEntry); 
    //
    usrModel.Countries = tempList.Select(C => new SelectListItem
    {
    Text = C.Country1,
    Value = C.CountryID.ToString()
    });


    var usrModel = new UserRegisterModel();
    usrModel.Countries = snwe.Countries..AsEnumerable().Select(C => new SelectListItem
    {
        Text = C.Country1,
        Value = C.CountryID.ToString(),
        Selected = "INDIA Country ID"
    });



    -KR


    -KR


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

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