如何在 MVC 中为国家和州/省创建选择列表 [英] How to create Select List for Country and States/province in MVC

查看:19
本文介绍了如何在 MVC 中为国家和州/省创建选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MVC 的新手,甚至是 asp ..

我想在 MVC 中创建一个表单.在一些示例的帮助下,我能够创建文本框,但我现在不明白如何创建选择列表./

我尝试搜索了许多在 MVC 中实现 Select List 的示例,但我无法理解.

我有一个表单,它一半用 HTML 编码,一半用 MVC 编码.

这是我的代码:

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;命名空间 MedAvail.Applications.MedProvision.Web.Models{公共类 AddressViewModel{公共字符串 Street1 { 获取;放;}公共字符串 Street2 { 获取;放;}公共字符串城市{得到;放;}公共字符串省{得到;放;}公共字符串国家{获取;放;}公共字符串邮政编码 { 获取;放;}公共字符串电话号码 { 获取;放;}}}<form id="locationInfo"><h1>位置信息</h1><表格宽度="80%" id="locInfo"><colgroup><col width="20%"/><col/></colgroup><tr><th>@Html.Label("Country")</th><td><select required=""><option>选择国家</option><option>加拿大</option><option>美国</option></选择><span class="required">*</span></td></tr><tr><th>@Html.LabelFor(x=>x.State)</th><td><select required=""><option>选择状态</option><option>状态 1</option><option>状态 2</option><option>状态 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><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"/>

</表单>

请指导我如何为此类表单创建选择列表.

解决方案

谢谢大家!我现在可以按照 MVC 加载选择列表我的工作代码如下:

视图中的 HTML+MVC 代码:-

 <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"文件夹下创建一个控制器:代码:-

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.Mvc;命名空间 MedAvail.Applications.MedProvision.Web.Util{公共类 SelectListItemHelper{公共静态 IEnumerable获取省份列表(){IListitems = new List{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"}};退换货品;}公共静态 IEnumerable获取国家列表(){IListitems = new List{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"}};退换货品;}}}

现在它的工作很酷:-)

谢谢!!

Hi I am new to MVC and even asp..

I want to create a form in MVC. With the help of some examples I am able to create TextBoxes, but I now I don't understand how to create Select List./

I tried searching many examples for implementing Select List in MVC, but I am not able to understand.

I have a Form which is half coded in HTML and half in MVC.

Here is my Code:

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>

Please guide me on how to create the Select List for this kind of form.

解决方案

Thank You All! I am able to to load Select List as per MVC now My Working Code is below:

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>

Created a Controller under "UTIL" folder: Code:-

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;
        }


    }
}

And its working COOL now :-)

Thank you!!

这篇关于如何在 MVC 中为国家和州/省创建选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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