asp.net 如何在MVC中创建国家和州/省的选择列表

r7xajy2e  于 2023-10-21  发布在  .NET
关注(0)|答案(8)|浏览(100)

你好,我是MVC的新手,甚至ASP。
我想在MVC中创建一个表单。在一些例子的帮助下,我能够创建文本框,但我现在不明白如何创建选择列表。
我试着搜索了很多在MVC中实现选择列表的例子,但我无法理解。
我有一个表单,一半是HTML编码,一半是MVC编码。
下面是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MedAvail.Applications.MedProvision.Web.Models
{
    public class AddressViewModel
    {
        public string Street1 { get; set; }
        public string Street2 { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        public string Country { get; set; }
        public string PostalCode { get; set; }
        public string PhoneNumber { get; set; }
    }
}



<form id="locationInfo">
    <h1>Location Information</h1>
    <table width="80%" id="locInfo">
        <colgroup>
            <col width="20%" />
            <col />
        </colgroup>
        <tr>
            <th>@Html.Label("Country")</th>
            <td>
                <select required="">
                    <option>Select Country</option>
                    <option>Canada</option>
                    <option>United States</option>
                </select>
                <span class="required">*</span>
            </td>
        </tr>
        <tr>
            <th>@Html.LabelFor(x=>x.State)</th>
            <td>
                <select required="">
                    <option>Select State</option>
                    <option>State 1</option>
                    <option>State 2</option>
                    <option>State 3</option>
                        ...............
                </select><span class="required">*</span></td>
        </tr>
        <tr>
            <th>@Html.LabelFor(x=>x.PostalCode)</th>
            <td>@Html.TextBoxFor(x=>x.PostalCode)<span class="required">*</span></td>
        </tr>
        <tr>
            <th>@Html.LabelFor(x=>x.City)</th>
            <td>@Html.TextBoxFor(x=>x.City)<span class="required">*</span></td>
        </tr>

        <tr>
            <th>@Html.LabelFor(x=>x.StreetAddress1)</th>
            <td>@Html.TextBoxFor(x=>x.StreetAddress1)<span class="required">*</span></td>
        </tr>
        <tr>
            <th>@Html.LabelFor(x=>x.StreetAddress2)</th>
            <td>@Html.TextBoxFor(x=>x.StreetAddress2)</td>
        </tr>
        <tr>
            <th>@Html.LabelFor(x=>x.PhoneNumber)</th>
            <td>@Html.TextBoxFor(x=>x.PhoneNumber)</td>
        </tr>

    </table>

    <div role="button" class="marginTop50 marginBottom">
        <input type="button" id="step3Back" value="Back" class="active" />
        <input type="button" id="step3confirmNext" value="Next" class="active marginLeft50" />
    </div>
</form>

请指导我如何创建这种形式的选择列表。

w8biq8rn

w8biq8rn1#

public static List<SelectListItem> States = new List<SelectListItem>()
    {
        new SelectListItem() {Text="Alabama", Value="AL"},
        new SelectListItem() { Text="Alaska", Value="AK"},
        new SelectListItem() { Text="Arizona", Value="AZ"},
        new SelectListItem() { Text="Arkansas", Value="AR"},
        new SelectListItem() { Text="California", Value="CA"},
        new SelectListItem() { Text="Colorado", Value="CO"},
        new SelectListItem() { Text="Connecticut", Value="CT"},
        new SelectListItem() { Text="District of Columbia", Value="DC"},
        new SelectListItem() { Text="Delaware", Value="DE"},
        new SelectListItem() { Text="Florida", Value="FL"},
        new SelectListItem() { Text="Georgia", Value="GA"},
        new SelectListItem() { Text="Hawaii", Value="HI"},
        new SelectListItem() { Text="Idaho", Value="ID"},
        new SelectListItem() { Text="Illinois", Value="IL"},
        new SelectListItem() { Text="Indiana", Value="IN"},
        new SelectListItem() { Text="Iowa", Value="IA"},
        new SelectListItem() { Text="Kansas", Value="KS"},
        new SelectListItem() { Text="Kentucky", Value="KY"},
        new SelectListItem() { Text="Louisiana", Value="LA"},
        new SelectListItem() { Text="Maine", Value="ME"},
        new SelectListItem() { Text="Maryland", Value="MD"},
        new SelectListItem() { Text="Massachusetts", Value="MA"},
        new SelectListItem() { Text="Michigan", Value="MI"},
        new SelectListItem() { Text="Minnesota", Value="MN"},
        new SelectListItem() { Text="Mississippi", Value="MS"},
        new SelectListItem() { Text="Missouri", Value="MO"},
        new SelectListItem() { Text="Montana", Value="MT"},
        new SelectListItem() { Text="Nebraska", Value="NE"},
        new SelectListItem() { Text="Nevada", Value="NV"},
        new SelectListItem() { Text="New Hampshire", Value="NH"},
        new SelectListItem() { Text="New Jersey", Value="NJ"},
        new SelectListItem() { Text="New Mexico", Value="NM"},
        new SelectListItem() { Text="New York", Value="NY"},
        new SelectListItem() { Text="North Carolina", Value="NC"},
        new SelectListItem() { Text="North Dakota", Value="ND"},
        new SelectListItem() { Text="Ohio", Value="OH"},
        new SelectListItem() { Text="Oklahoma", Value="OK"},
        new SelectListItem() { Text="Oregon", Value="OR"},
        new SelectListItem() { Text="Pennsylvania", Value="PA"},
        new SelectListItem() { Text="Rhode Island", Value="RI"},
        new SelectListItem() { Text="South Carolina", Value="SC"},
        new SelectListItem() { Text="South Dakota", Value="SD"},
        new SelectListItem() { Text="Tennessee", Value="TN"},
        new SelectListItem() { Text="Texas", Value="TX"},
        new SelectListItem() { Text="Utah", Value="UT"},
        new SelectListItem() { Text="Vermont", Value="VT"},
        new SelectListItem() { Text="Virginia", Value="VA"},
        new SelectListItem() { Text="Washington", Value="WA"},
        new SelectListItem() { Text="West Virginia", Value="WV"},
        new SelectListItem() { Text="Wisconsin", Value="WI"},
        new SelectListItem() { Text="Wyoming", Value="WY"}
    };

我们的做法是把这个方法放到一个类中,然后从视图中调用这个类

@Html.DropDownListFor(x => x.State, Class.States)
tuwxkamq

tuwxkamq2#

谢谢大家!我现在能够按照MVC加载选择列表我的工作代码如下:
HTML+MVC Code in View:-

<tr>
        <th>@Html.Label("Country")</th>
        <td>@Html.DropDownListFor(x =>x.Province,SelectListItemHelper.GetCountryList())<span class="required">*</span></td>
    </tr>
    <tr>
        <th>@Html.LabelFor(x=>x.Province)</th>
        <td>@Html.DropDownListFor(x =>x.Province,SelectListItemHelper.GetProvincesList())<span class="required">*</span></td>
    </tr>

在“UTIL”文件夹下创建了一个Controller:代码:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MedAvail.Applications.MedProvision.Web.Util
{
    public class SelectListItemHelper
    {
        public static IEnumerable<SelectListItem> GetProvincesList()
        {
            IList<SelectListItem> items = new List<SelectListItem>
            {
                new SelectListItem{Text = "California", Value = "B"},
                new SelectListItem{Text = "Alaska", Value = "B"},
                new SelectListItem{Text = "Illinois", Value = "B"},
                new SelectListItem{Text = "Texas", Value = "B"},
                new SelectListItem{Text = "Washington", Value = "B"}

            };
            return items;
        }

        public static IEnumerable<SelectListItem> GetCountryList()
        {
            IList<SelectListItem> items = new List<SelectListItem>
            {
                new SelectListItem{Text = "United States", Value = "B"},
                new SelectListItem{Text = "Canada", Value = "B"},
                new SelectListItem{Text = "United Kingdom", Value = "B"},
                new SelectListItem{Text = "Texas", Value = "B"},
                new SelectListItem{Text = "Washington", Value = "B"}

            };
            return items;
        }

    }
}

现在它的工作很酷:-)
感谢您的评分!

xurqigkl

xurqigkl3#

谢谢你的这
我是这样做的:
1.已在Utils文件夹中创建Extensions.cs文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.ProjectName.Utils
{
    public class Extensions
    {
        public static IEnumerable<SelectListItem> GetStatesList()
        {
            IList<SelectListItem> states = new List<SelectListItem>
            {
                new SelectListItem() {Text="Alabama", Value="AL"},
                new SelectListItem() { Text="Alaska", Value="AK"},
                new SelectListItem() { Text="Arizona", Value="AZ"},
                new SelectListItem() { Text="Arkansas", Value="AR"},
                new SelectListItem() { Text="California", Value="CA"},
                new SelectListItem() { Text="Colorado", Value="CO"},
                new SelectListItem() { Text="Connecticut", Value="CT"},
                new SelectListItem() { Text="District of Columbia", Value="DC"},
                new SelectListItem() { Text="Delaware", Value="DE"},
                new SelectListItem() { Text="Florida", Value="FL"},
                new SelectListItem() { Text="Georgia", Value="GA"},
                new SelectListItem() { Text="Hawaii", Value="HI"},
                new SelectListItem() { Text="Idaho", Value="ID"},
                new SelectListItem() { Text="Illinois", Value="IL"},
                new SelectListItem() { Text="Indiana", Value="IN"},
                new SelectListItem() { Text="Iowa", Value="IA"},
                new SelectListItem() { Text="Kansas", Value="KS"},
                new SelectListItem() { Text="Kentucky", Value="KY"},
                new SelectListItem() { Text="Louisiana", Value="LA"},
                new SelectListItem() { Text="Maine", Value="ME"},
                new SelectListItem() { Text="Maryland", Value="MD"},
                new SelectListItem() { Text="Massachusetts", Value="MA"},
                new SelectListItem() { Text="Michigan", Value="MI"},
                new SelectListItem() { Text="Minnesota", Value="MN"},
                new SelectListItem() { Text="Mississippi", Value="MS"},
                new SelectListItem() { Text="Missouri", Value="MO"},
                new SelectListItem() { Text="Montana", Value="MT"},
                new SelectListItem() { Text="Nebraska", Value="NE"},
                new SelectListItem() { Text="Nevada", Value="NV"},
                new SelectListItem() { Text="New Hampshire", Value="NH"},
                new SelectListItem() { Text="New Jersey", Value="NJ"},
                new SelectListItem() { Text="New Mexico", Value="NM"},
                new SelectListItem() { Text="New York", Value="NY"},
                new SelectListItem() { Text="North Carolina", Value="NC"},
                new SelectListItem() { Text="North Dakota", Value="ND"},
                new SelectListItem() { Text="Ohio", Value="OH"},
                new SelectListItem() { Text="Oklahoma", Value="OK"},
                new SelectListItem() { Text="Oregon", Value="OR"},
                new SelectListItem() { Text="Pennsylvania", Value="PA"},
                new SelectListItem() { Text="Rhode Island", Value="RI"},
                new SelectListItem() { Text="South Carolina", Value="SC"},
                new SelectListItem() { Text="South Dakota", Value="SD"},
                new SelectListItem() { Text="Tennessee", Value="TN"},
                new SelectListItem() { Text="Texas", Value="TX"},
                new SelectListItem() { Text="Utah", Value="UT"},
                new SelectListItem() { Text="Vermont", Value="VT"},
                new SelectListItem() { Text="Virginia", Value="VA"},
                new SelectListItem() { Text="Washington", Value="WA"},
                new SelectListItem() { Text="West Virginia", Value="WV"},
                new SelectListItem() { Text="Wisconsin", Value="WI"},
                new SelectListItem() { Text="Wyoming", Value="WY"}
            };
            return states;
        }
    }
}

2.在我的模型中,状态将被缩写(例如,“AL”、“NY”等):

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Web.ProjectName.Models
{
    public class ContactForm
    {

        ...

        [Required]
        [Display(Name = "State")]
        [RegularExpression("[A-Z]{2}")]
        public string State { get; set; }

        ...

    }
}

2.在我看来,我引用了它:

@model Web.ProjectName.Models.ContactForm

  ...

  @Html.LabelFor(x => x.State, new { @class = "form-label" })
  @Html.DropDownListFor(x => x.State, Web.ProjectName.Utils.Extensions.GetStatesList(), new { @class = "form-control" })

  ...
3htmauhk

3htmauhk4#

设计您的模型:

Public class ModelName
{
    ...// Properties
    public IEnumerable<SelectListItem> ListName { get; set; }
}

准备List并将其绑定到Controller中的Model:

public ActionResult Index(ModelName model)
    {
        var items = // Your List of data
        model.ListName = items.Select(x=> new SelectListItem() {
                    Text = x.prop,
                    Value = x.prop2
               });
    }

在您看来:

@Html.DropDownListFor(m => Model.prop2,Model.ListName)
knpiaxh1

knpiaxh15#

这么多的方法来做到这一点...对于#NetCore,请使用Lib..

using System;
    using System.Collections.Generic;
    using System.Linq; // only required when .AsEnumerable() is used
    using System.ComponentModel.DataAnnotations;
    using Microsoft.AspNetCore.Mvc.Rendering;

模特...

namespace MyProject.Models
{
  public class MyModel
  {
    [Required]
    [Display(Name = "State")]
    public string StatePick { get; set; }
    public string state { get; set; }
    [StringLength(35, ErrorMessage = "State cannot be longer than 35 characters.")]
    public SelectList StateList { get; set; }
  }
}

控制器...

namespace MyProject.Controllers
{
    /// <summary>
    /// create SelectListItem from strings
    /// </summary>
    /// <param name="isValue">defaultValue is SelectListItem.Value is true, SelectListItem.Text if false</param>
    /// <returns></returns>
    private SelectListItem newItem(string value, string text, string defaultValue = "", bool isValue = false)
    {
        SelectListItem ss = new SelectListItem();
        ss.Text = text;
        ss.Value = value;

        // select default by Value or Text
        if (isValue && ss.Value == defaultValue || !isValue && ss.Text == defaultValue)
        {
            ss.Selected = true;
        }

        return ss;
    }

    /// <summary>
    /// this pulls the state name from _context and sets it as the default for the selectList
    /// </summary>
    /// <param name="myState">sets default value for list of states</param>
    /// <returns></returns>
    private SelectList getStateList(string myState = "")
    {
        List<SelectListItem> states = new List<SelectListItem>();
        SelectListItem chosen = new SelectListItem();

        // set default selected state to OHIO
        string defaultValue = "OH";
        if (!string.IsNullOrEmpty(myState))
        {
            defaultValue = myState;
        }

        try
        {
            states.Add(newItem("AL", "Alabama", defaultValue, true));
            states.Add(newItem("AK", "Alaska", defaultValue, true));
            states.Add(newItem("AZ", "Arizona", defaultValue, true));
            states.Add(newItem("AR", "Arkansas", defaultValue, true));
            states.Add(newItem("CA", "California", defaultValue, true));
            states.Add(newItem("CO", "Colorado", defaultValue, true));
            states.Add(newItem("CT", "Connecticut", defaultValue, true));
            states.Add(newItem("DE", "Delaware", defaultValue, true));
            states.Add(newItem("DC", "District of Columbia", defaultValue, true));
            states.Add(newItem("FL", "Florida", defaultValue, true));
            states.Add(newItem("GA", "Georgia", defaultValue, true));
            states.Add(newItem("HI", "Hawaii", defaultValue, true));
            states.Add(newItem("ID", "Idaho", defaultValue, true));
            states.Add(newItem("IL", "Illinois", defaultValue, true));
            states.Add(newItem("IN", "Indiana", defaultValue, true));
            states.Add(newItem("IA", "Iowa", defaultValue, true));
            states.Add(newItem("KS", "Kansas", defaultValue, true));
            states.Add(newItem("KY", "Kentucky", defaultValue, true));
            states.Add(newItem("LA", "Louisiana", defaultValue, true));
            states.Add(newItem("ME", "Maine", defaultValue, true));
            states.Add(newItem("MD", "Maryland", defaultValue, true));
            states.Add(newItem("MA", "Massachusetts", defaultValue, true));
            states.Add(newItem("MI", "Michigan", defaultValue, true));
            states.Add(newItem("MN", "Minnesota", defaultValue, true));
            states.Add(newItem("MS", "Mississippi", defaultValue, true));
            states.Add(newItem("MO", "Missouri", defaultValue, true));
            states.Add(newItem("MT", "Montana", defaultValue, true));
            states.Add(newItem("NE", "Nebraska", defaultValue, true));
            states.Add(newItem("NV", "Nevada", defaultValue, true));
            states.Add(newItem("NH", "New Hampshire", defaultValue, true));
            states.Add(newItem("NJ", "New Jersey", defaultValue, true));
            states.Add(newItem("NM", "New Mexico", defaultValue, true));
            states.Add(newItem("NY", "New York", defaultValue, true));
            states.Add(newItem("NC", "North Carolina", defaultValue, true));
            states.Add(newItem("ND", "North Dakota", defaultValue, true));
            states.Add(newItem("OH", "Ohio", defaultValue, true));
            states.Add(newItem("OK", "Oklahoma", defaultValue, true));
            states.Add(newItem("OR", "Oregon", defaultValue, true));
            states.Add(newItem("PA", "Pennsylvania", defaultValue, true));
            states.Add(newItem("RI", "Rhode Island", defaultValue, true));
            states.Add(newItem("SC", "South Carolina", defaultValue, true));
            states.Add(newItem("SD", "South Dakota", defaultValue, true));
            states.Add(newItem("TN", "Tennessee", defaultValue, true));
            states.Add(newItem("TX", "Texas", defaultValue, true));
            states.Add(newItem("UT", "Utah", defaultValue, true));
            states.Add(newItem("VT", "Vermont", defaultValue, true));
            states.Add(newItem("VA", "Virginia", defaultValue, true));
            states.Add(newItem("WA", "Washington", defaultValue, true));
            states.Add(newItem("WV", "West Virginia", defaultValue, true));
            states.Add(newItem("WI", "Wisconsin", defaultValue, true));
            states.Add(newItem("WY", "Wyoming", defaultValue, true));

            foreach (SelectListItem state in states)
            {
                if (state.Selected)
                {
                    chosen = state;
                    break;
                }
            }
        }
        catch (Exception err)
        {
            string ss = "ERR!   " + err.Source + "   " + err.GetType().ToString() + "\r\n" + err.Message.Replace("\r\n", "   ");
            ss = this.sendError("Online getStateList Request", ss, _errPassword);
            // return error
        }

        // .AsEnumerable() is not required in the pass.. it is an extension of Linq
        SelectList myList = new SelectList(states.AsEnumerable(), "Value", "Text", chosen);

        object val = myList.SelectedValue;

        return myList;
    }

    public ActionResult pickState(MyModel pData)
    {
        if (pData.StateList == null)
        {
            if (String.IsNullOrEmpty(pData.StatePick)) // state abbrev, value collected onchange
            {
                pData.StateList = getStateList();
            }
            else
            {
                pData.StateList = getStateList(pData.StatePick);
            }

            // assign values to state list items
            try
            {
                SelectListItem si = (SelectListItem)pData.StateList.SelectedValue;
                pData.state = si.Value;
                pData.StatePick = si.Value;
            }
            catch { }
        } 
    return View(pData);
    }
}

pickState.cshtml.

@model MyProject.Models.MyModel

@{
ViewBag.Title = "United States of America";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title - Where are you...</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>

    <div class="editor-label">
        @Html.DisplayNameFor(model => model.state)
    </div>
    <div class="display-field">            
        @Html.DropDownListFor(m => m.StatePick, Model.StateList, new { OnChange = "state.value = this.value;" })
        @Html.EditorFor(model => model.state)
        @Html.ValidationMessageFor(model => model.StateList)
    </div>         
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>
f0brbegy

f0brbegy6#

我也喜欢乔丹的回答,并自己实现了它。我只需要缩写,以便在情况下别人需要相同的:

public static IEnumerable<SelectListItem> GetStatesList()
    {
        IList<SelectListItem> states = new List<SelectListItem>
        {
            new SelectListItem() {Text="AL", Value="AL"},
            new SelectListItem() { Text="AK", Value="AK"},
            new SelectListItem() { Text="AZ", Value="AZ"},
            new SelectListItem() { Text="AR", Value="AR"},
            new SelectListItem() { Text="CA", Value="CA"},
            new SelectListItem() { Text="CO", Value="CO"},
            new SelectListItem() { Text="CT", Value="CT"},
            new SelectListItem() { Text="DC", Value="DC"},
            new SelectListItem() { Text="DE", Value="DE"},
            new SelectListItem() { Text="FL", Value="FL"},
            new SelectListItem() { Text="GA", Value="GA"},
            new SelectListItem() { Text="HI", Value="HI"},
            new SelectListItem() { Text="ID", Value="ID"},
            new SelectListItem() { Text="IL", Value="IL"},
            new SelectListItem() { Text="IN", Value="IN"},
            new SelectListItem() { Text="IA", Value="IA"},
            new SelectListItem() { Text="KS", Value="KS"},
            new SelectListItem() { Text="KY", Value="KY"},
            new SelectListItem() { Text="LA", Value="LA"},
            new SelectListItem() { Text="ME", Value="ME"},
            new SelectListItem() { Text="MD", Value="MD"},
            new SelectListItem() { Text="MA", Value="MA"},
            new SelectListItem() { Text="MI", Value="MI"},
            new SelectListItem() { Text="MN", Value="MN"},
            new SelectListItem() { Text="MS", Value="MS"},
            new SelectListItem() { Text="MO", Value="MO"},
            new SelectListItem() { Text="MT", Value="MT"},
            new SelectListItem() { Text="NE", Value="NE"},
            new SelectListItem() { Text="NV", Value="NV"},
            new SelectListItem() { Text="NH", Value="NH"},
            new SelectListItem() { Text="NJ", Value="NJ"},
            new SelectListItem() { Text="NM", Value="NM"},
            new SelectListItem() { Text="NY", Value="NY"},
            new SelectListItem() { Text="NC", Value="NC"},
            new SelectListItem() { Text="ND", Value="ND"},
            new SelectListItem() { Text="OH", Value="OH"},
            new SelectListItem() { Text="OK", Value="OK"},
            new SelectListItem() { Text="OR", Value="OR"},
            new SelectListItem() { Text="PA", Value="PA"},
            new SelectListItem() { Text="PR", Value="PR"},
            new SelectListItem() { Text="RI", Value="RI"},
            new SelectListItem() { Text="SC", Value="SC"},
            new SelectListItem() { Text="SD", Value="SD"},
            new SelectListItem() { Text="TN", Value="TN"},
            new SelectListItem() { Text="TX", Value="TX"},
            new SelectListItem() { Text="UT", Value="UT"},
            new SelectListItem() { Text="VT", Value="VT"},
            new SelectListItem() { Text="VA", Value="VA"},
            new SelectListItem() { Text="WA", Value="WA"},
            new SelectListItem() { Text="WV", Value="WV"},
            new SelectListItem() { Text="WI", Value="WI"},
            new SelectListItem() { Text="WY", Value="WY"}
        };
        return states;
    }
2exbekwf

2exbekwf7#

如果你想要一份国家名单
public void run(){

IList<SelectListItem> countries = new List<SelectListItem>
 {
     new SelectListItem{Value="AF", Text="Afghanistan"},
     new SelectListItem{Value="AX", Text="Aland Islands"},
     new SelectListItem{Value="AL", Text="Albania"},
     new SelectListItem{Value="DZ", Text="Algeria"},
     new SelectListItem{Value="AS", Text="American Samoa"},
     new SelectListItem{Value="AD", Text="Andorra"},
     new SelectListItem{Value="AO", Text="Angola"},
     new SelectListItem{Value="AI", Text="Anguilla"},
     new SelectListItem{Value="AQ", Text="Antarctica"},
     new SelectListItem{Value="AG", Text="Antigua and Barbuda"},
     new SelectListItem{Value="AR", Text="Argentina"},
     new SelectListItem{Value="AM", Text="Armenia"},
     new SelectListItem{Value="AW", Text="Aruba"},
     new SelectListItem{Value="AU", Text="Australia"},
     new SelectListItem{Value="AT", Text="Austria"},
     new SelectListItem{Value="AZ", Text="Azerbaijan"},
     new SelectListItem{Value="BS", Text="Bahamas"},
     new SelectListItem{Value="BH", Text="Bahrain"},
     new SelectListItem{Value="BD", Text="Bangladesh"},
     new SelectListItem{Value="BB", Text="Barbados"},
     new SelectListItem{Value="BY", Text="Belarus"},
     new SelectListItem{Value="BE", Text="Belgium"},
     new SelectListItem{Value="BZ", Text="Belize"},
     new SelectListItem{Value="BJ", Text="Benin"},
     new SelectListItem{Value="BM", Text="Bermuda"},
     new SelectListItem{Value="BT", Text="Bhutan"},
     new SelectListItem{Value="BO", Text="Bolivia"},
     new SelectListItem{Value="BQ", Text="Bonaire, Sint Eustatius and Saba"},
     new SelectListItem{Value="BA", Text="Bosnia and Herzegovina"},
     new SelectListItem{Value="BW", Text="Botswana"},
     new SelectListItem{Value="BV", Text="Bouvet Island"},
     new SelectListItem{Value="BR", Text="Brazil"},
     new SelectListItem{Value="IO", Text="British Indian Ocean Territory"},
     new SelectListItem{Value="BN", Text="Brunei Darussalam"},
     new SelectListItem{Value="BG", Text="Bulgaria"},
     new SelectListItem{Value="BF", Text="Burkina Faso"},
     new SelectListItem{Value="BI", Text="Burundi"},
     new SelectListItem{Value="KH", Text="Cambodia"},
     new SelectListItem{Value="CM", Text="Cameroon"},
     new SelectListItem{Value="CA", Text="Canada"},
     new SelectListItem{Value="CV", Text="Cape Verde"},
     new SelectListItem{Value="KY", Text="Cayman Islands"},
     new SelectListItem{Value="CF", Text="Central African Republic"},
     new SelectListItem{Value="TD", Text="Chad"},
     new SelectListItem{Value="CL", Text="Chile"},
     new SelectListItem{Value="CN", Text="China"},
     new SelectListItem{Value="CX", Text="Christmas Island"},
     new SelectListItem{Value="CC", Text="Cocos(Keeling) Islands"},
     new SelectListItem{Value="CO", Text="Colombia"},
     new SelectListItem{Value="KM", Text="Comoros"},
     new SelectListItem{Value="CG", Text="Congo"},
     new SelectListItem{Value="CD", Text="Congo, Democratic Republic of the Congo"},
     new SelectListItem{Value="CK", Text="Cook Islands"},
     new SelectListItem{Value="CR", Text="Costa Rica"},
     new SelectListItem{Value="CI", Text="Cote D'Ivoire"},
     new SelectListItem{Value="HR", Text="Croatia"},
     new SelectListItem{Value="CU", Text="Cuba"},
     new SelectListItem{Value="CW", Text="Curacao"},
     new SelectListItem{Value="CY", Text="Cyprus"},
     new SelectListItem{Value="CZ", Text="Czech Republic"},
     new SelectListItem{Value="DK", Text="Denmark"},
     new SelectListItem{Value="DJ", Text="Djibouti"},
     new SelectListItem{Value="DM", Text="Dominica"},
     new SelectListItem{Value="DO", Text="Dominican Republic"},
     new SelectListItem{Value="EC", Text="Ecuador"},
     new SelectListItem{Value="EG", Text="Egypt"},
     new SelectListItem{Value="SV", Text="El Salvador"},
     new SelectListItem{Value="GQ", Text="Equatorial Guinea"},
     new SelectListItem{Value="ER", Text="Eritrea"},
     new SelectListItem{Value="EE", Text="Estonia"},
     new SelectListItem{Value="ET", Text="Ethiopia"},
     new SelectListItem{Value="FK", Text="Falkland Islands (Malvinas)"},
     new SelectListItem{Value="FO", Text="Faroe Islands"},
     new SelectListItem{Value="FJ", Text="Fiji"},
     new SelectListItem{Value="FI", Text="Finland"},
     new SelectListItem{Value="FR", Text="France"},
     new SelectListItem{Value="GF", Text="French Guiana"},
     new SelectListItem{Value="PF", Text="French Polynesia"},
     new SelectListItem{Value="TF", Text="French Southern Territories"},
     new SelectListItem{Value="GA", Text="Gabon"},
     new SelectListItem{Value="GM", Text="Gambia"},
     new SelectListItem{Value="GE", Text="Georgia"},
     new SelectListItem{Value="DE", Text="Germany"},
     new SelectListItem{Value="GH", Text="Ghana"},
     new SelectListItem{Value="GI", Text="Gibraltar"},
     new SelectListItem{Value="GR", Text="Greece"},
     new SelectListItem{Value="GL", Text="Greenland"},
     new SelectListItem{Value="GD", Text="Grenada"},
     new SelectListItem{Value="GP", Text="Guadeloupe"},
     new SelectListItem{Value="GU", Text="Guam"},
     new SelectListItem{Value="GT", Text="Guatemala"},
     new SelectListItem{Value="GG", Text="Guernsey"},
     new SelectListItem{Value="GN", Text="Guinea"},
     new SelectListItem{Value="GW", Text="Guinea - Bissau"},
     new SelectListItem{Value="GY", Text="Guyana"},
     new SelectListItem{Value="HT", Text="Haiti"},
     new SelectListItem{Value="HM", Text="Heard Island and Mcdonald Islands"},
     new SelectListItem{Value="VA", Text="Holy See (Vatican City State)"},
     new SelectListItem{Value="HN", Text="Honduras"},
     new SelectListItem{Value="HK", Text="Hong Kong"},
     new SelectListItem{Value="HU", Text="Hungary"},
     new SelectListItem{Value="IS", Text="Iceland"},
     new SelectListItem{Value="IN", Text="India"},
     new SelectListItem{Value="ID", Text="Indonesia"},
     new SelectListItem{Value="IR", Text="Iran, Islamic Republic of"},
     new SelectListItem{Value="IQ", Text="Iraq"},
     new SelectListItem{Value="IE", Text="Ireland"},
     new SelectListItem{Value="IM", Text="Isle of Man"},
     new SelectListItem{Value="IL", Text="Israel"},
     new SelectListItem{Value="IT", Text="Italy"},
     new SelectListItem{Value="JM", Text="Jamaica"},
     new SelectListItem{Value="JP", Text="Japan"},
     new SelectListItem{Value="JE", Text="Jersey"},
     new SelectListItem{Value="JO", Text="Jordan"},
     new SelectListItem{Value="KZ", Text="Kazakhstan"},
     new SelectListItem{Value="KE", Text="Kenya"},
     new SelectListItem{Value="KI", Text="Kiribati"},
     new SelectListItem{Value="KP", Text="Korea, Democratic People's Republic of"},
     new SelectListItem{Value="KR", Text="Korea, Republic of"},
     new SelectListItem{Value="XK", Text="Kosovo"},
     new SelectListItem{Value="KW", Text="Kuwait"},
     new SelectListItem{Value="KG", Text="Kyrgyzstan"},
     new SelectListItem{Value="LA", Text="Lao People's Democratic Republic"},
     new SelectListItem{Value="LV", Text="Latvia"},
     new SelectListItem{Value="LB", Text="Lebanon"},
     new SelectListItem{Value="LS", Text="Lesotho"},
     new SelectListItem{Value="LR", Text="Liberia"},
     new SelectListItem{Value="LY", Text="Libyan Arab Jamahiriya"},
     new SelectListItem{Value="LI", Text="Liechtenstein"},
     new SelectListItem{Value="LT", Text="Lithuania"},
     new SelectListItem{Value="LU", Text="Luxembourg"},
     new SelectListItem{Value="MO", Text="Macao"},
     new SelectListItem{Value="MK", Text="Macedonia, the Former Yugoslav Republic of"},
     new SelectListItem{Value="MG", Text="Madagascar"},
     new SelectListItem{Value="MW", Text="Malawi"},
     new SelectListItem{Value="MY", Text="Malaysia"},
     new SelectListItem{Value="MV", Text="Maldives"},
     new SelectListItem{Value="ML", Text="Mali"},
     new SelectListItem{Value="MT", Text="Malta"},
     new SelectListItem{Value="MH", Text="Marshall Islands"},
     new SelectListItem{Value="MQ", Text="Martinique"},
     new SelectListItem{Value="MR", Text="Mauritania"},
     new SelectListItem{Value="MU", Text="Mauritius"},
     new SelectListItem{Value="YT", Text="Mayotte"},
     new SelectListItem{Value="MX", Text="Mexico"},
     new SelectListItem{Value="FM", Text="Micronesia, Federated States of"},
     new SelectListItem{Value="MD", Text="Moldova, Republic of"},
     new SelectListItem{Value="MC", Text="Monaco"},
     new SelectListItem{Value="MN", Text="Mongolia"},
     new SelectListItem{Value="ME", Text="Montenegro"},
     new SelectListItem{Value="MS", Text="Montserrat"},
     new SelectListItem{Value="MA", Text="Morocco"},
     new SelectListItem{Value="MZ", Text="Mozambique"},
     new SelectListItem{Value="MM", Text="Myanmar"},
     new SelectListItem{Value="NA", Text="Namibia"},
     new SelectListItem{Value="NR", Text="Nauru"},
     new SelectListItem{Value="NP", Text="Nepal"},
     new SelectListItem{Value="NL", Text="Netherlands"},
     new SelectListItem{Value="AN", Text="Netherlands Antilles"},
     new SelectListItem{Value="NC", Text="New Caledonia"},
     new SelectListItem{Value="NZ", Text="New Zealand"},
     new SelectListItem{Value="NI", Text="Nicaragua"},
     new SelectListItem{Value="NE", Text="Niger"},
     new SelectListItem{Value="NG", Text="Nigeria"},
     new SelectListItem{Value="NU", Text="Niue"},
     new SelectListItem{Value="NF", Text="Norfolk Island"},
     new SelectListItem{Value="MP", Text="Northern Mariana Islands"},
     new SelectListItem{Value="NO", Text="Norway"},
     new SelectListItem{Value="OM", Text="Oman"},
     new SelectListItem{Value="PK", Text="Pakistan"},
     new SelectListItem{Value="PW", Text="Palau"},
     new SelectListItem{Value="PS", Text="Palestinian Territory, Occupied"},
     new SelectListItem{Value="PA", Text="Panama"},
     new SelectListItem{Value="PG", Text="Papua New Guinea"},
     new SelectListItem{Value="PY", Text="Paraguay"},
     new SelectListItem{Value="PE", Text="Peru"},
     new SelectListItem{Value="PH", Text="Philippines"},
     new SelectListItem{Value="PN", Text="Pitcairn"},
     new SelectListItem{Value="PL", Text="Poland"},
     new SelectListItem{Value="PT", Text="Portugal"},
     new SelectListItem{Value="PR", Text="Puerto Rico"},
     new SelectListItem{Value="QA", Text="Qatar"},
     new SelectListItem{Value="RE", Text="Reunion"},
     new SelectListItem{Value="RO", Text="Romania"},
     new SelectListItem{Value="RU", Text="Russian Federation"},
     new SelectListItem{Value="RW", Text="Rwanda"},
     new SelectListItem{Value="BL", Text="Saint Barthelemy"},
     new SelectListItem{Value="SH", Text="Saint Helena"},
     new SelectListItem{Value="KN", Text="Saint Kitts and Nevis"},
     new SelectListItem{Value="LC", Text="Saint Lucia"},
     new SelectListItem{Value="MF", Text="Saint Martin"},
     new SelectListItem{Value="PM", Text="Saint Pierre and Miquelon"},
     new SelectListItem{Value="VC", Text="Saint Vincent and the Grenadines"},
     new SelectListItem{Value="WS", Text="Samoa"},
     new SelectListItem{Value="SM", Text="San Marino"},
     new SelectListItem{Value="ST", Text="Sao Tome and Principe"},
     new SelectListItem{Value="SA", Text="Saudi Arabia"},
     new SelectListItem{Value="SN", Text="Senegal"},
     new SelectListItem{Value="RS", Text="Serbia"},
     new SelectListItem{Value="CS", Text="Serbia and Montenegro"},
     new SelectListItem{Value="SC", Text="Seychelles"},
     new SelectListItem{Value="SL", Text="Sierra Leone"},
     new SelectListItem{Value="SG", Text="Singapore"},
     new SelectListItem{Value="SX", Text="Sint Maarten"},
     new SelectListItem{Value="SK", Text="Slovakia"},
     new SelectListItem{Value="SI", Text="Slovenia"},
     new SelectListItem{Value="SB", Text="Solomon Islands"},
     new SelectListItem{Value="SO", Text="Somalia"},
     new SelectListItem{Value="ZA", Text="South Africa"},
     new SelectListItem{Value="GS", Text="South Georgia and the South Sandwich Islands"},
     new SelectListItem{Value="SS", Text="South Sudan"},
     new SelectListItem{Value="ES", Text="Spain"},
     new SelectListItem{Value="LK", Text="Sri Lanka"},
     new SelectListItem{Value="SD", Text="Sudan"},
     new SelectListItem{Value="SR", Text="Suriname"},
     new SelectListItem{Value="SJ", Text="Svalbard and Jan Mayen"},
     new SelectListItem{Value="SZ", Text="Swaziland"},
     new SelectListItem{Value="SE", Text="Sweden"},
     new SelectListItem{Value="CH", Text="Switzerland"},
     new SelectListItem{Value="SY", Text="Syrian Arab Republic"},
     new SelectListItem{Value="TW", Text="Taiwan, Province of China"},
     new SelectListItem{Value="TJ", Text="Tajikistan"},
     new SelectListItem{Value="TZ", Text="Tanzania, United Republic of"},
     new SelectListItem{Value="TH", Text="Thailand"},
     new SelectListItem{Value="TL", Text="Timor - Leste"},
     new SelectListItem{Value="TG", Text="Togo"},
     new SelectListItem{Value="TK", Text="Tokelau"},
     new SelectListItem{Value="TO", Text="Tonga"},
     new SelectListItem{Value="TT", Text="Trinidad and Tobago"},
     new SelectListItem{Value="TN", Text="Tunisia"},
     new SelectListItem{Value="TR", Text="Turkey"},
     new SelectListItem{Value="TM", Text="Turkmenistan"},
     new SelectListItem{Value="TC", Text="Turks and Caicos Islands"},
     new SelectListItem{Value="TV", Text="Tuvalu"},
     new SelectListItem{Value="UG", Text="Uganda"},
     new SelectListItem{Value="UA", Text="Ukraine"},
     new SelectListItem{Value="AE", Text="United Arab Emirates"},
     new SelectListItem{Value="GB", Text="United Kingdom"},
     new SelectListItem{Value="US", Text="United States"},
     new SelectListItem{Value="UM", Text="United States Minor Outlying Islands"},
     new SelectListItem{Value="UY", Text="Uruguay"},
     new SelectListItem{Value="UZ", Text="Uzbekistan"},
     new SelectListItem{Value="VU", Text="Vanuatu"},
     new SelectListItem{Value="VE", Text="Venezuela"},
     new SelectListItem{Value="VN", Text="Viet Nam"},
     new SelectListItem{Value="VG", Text="Virgin Islands, British"},
     new SelectListItem{Value="VI", Text="Virgin Islands, U.s."},
     new SelectListItem{Value="WF", Text="Wallis and Futuna"},
     new SelectListItem{Value="EH", Text="Western Sahara"},
     new SelectListItem{Value="YE", Text="Yemen"},
     new SelectListItem{Value="ZM", Text="Zambia"},
     new SelectListItem{Value="ZW", Text="Zimbabwe"}

 };
 return countries;

}

am46iovg

am46iovg8#

制作下拉列表的最佳方法:

grid.Column("PriceType",canSort:true,header: "PriceType",format: @<span>
    <span id="[email protected]">@item.PriceTypeDescription</span>
    @Html.DropDownList("PriceType"+(int)item.ShoppingCartID,new SelectList(MvcApplication1.Services.ExigoApiContext.CreateODataContext().PriceTypes.Select(s => new { s.PriceTypeID, s.PriceTypeDescription }).AsEnumerable(),"PriceTypeID", "PriceTypeDescription",Convert.ToInt32(item.PriceTypeId)), new { @class = "PriceType",@style="width:120px;display:none",@selectedvalue="selected"})
        </span>),

相关问题