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

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

问题描述

您好我是新来的MVC,甚至ASP。

Hi I am new to MVC and even asp..

我想创建MVC形式。随着一些示例的帮助下,我能够创建文本框,但是我现在我不知道如何创建选择列表./

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./

我想在MVC实施选择列表搜索的例子很多,但我无法理解。

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

我有一个表格这是在HTML一半codeD和一半的MVC。

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

下面是我的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.

推荐答案

感谢大家!我能够加载选择列表按照MVC我现在的工作code是如下:

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

HTML + MVC code在View: -

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文件夹中的控制器:
code: -

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


    }
}

而现在它的工作COOL: - )

And its working COOL now :-)

感谢您!!

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

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