你好,我是mvc甚至asp的新手.

我想在MVC中创建一个表单.借助一些示例,我能够创建文本框,但我现在不知道如何创建 Select 列表/

我试着搜索了很多在MVC中实现 Select 列表的例子,但我不理解.

我有一个表单,一半用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>

请指导我如何创建此类表单的 Select 列表.

推荐答案

谢谢大家!我能够按照MVC加载 Select 列表,现在我的工作代码如下:

视图中的HTML+MVC代码:-

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

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


    }
}

现在工作起来很酷:-)

非常感谢.

Asp.net相关问答推荐

您如何处理多个环境的多个 web.config 文件?

如何在 ASP.NET 中以 'YYYY-MM-DD' 格式获取当前日期?

ASP.NET GridView 第二个标题行跨越主标题行

Web api 不支持 POST 方法

用于链接字符串中的 url 的 C# 代码

我应该使用用户名还是用户 ID 来引用 ASP.NET 中经过身份验证的用户

如何计算/查找给定日期的周数?

如何使用文件上传控件 Select 多个文件?

如何在不使用 Session 的情况下在 ASP.net 中的页面之间传递值

使用 ASP.NET WebForms 的 jQuery DataTables 服务器端处理

IIS 会话超时与 ASP.NET 会话超时

在生产中使用 LocalDb 是否正常?

ASP.NET IIS Web.config [内部服务器错误]

如何在 ASP.Net 的客户端 (JavaScript) 上判断 Page.Validate()?

Owin.IAppBuilder不包含MapSignalR的定义

将新行添加到数据表的顶部

Html.RenderAction 和 Html.Action 的区别

如何遍历自定义 vb.net 对象的每个属性?

在 ASP.NET 标识中添加角色

使用 FormsAuthentication.SetAuthCookie 存储更多信息