MVC2 Html.DropDownList数据绑定

一步:新建一个mvc 2 工程

 

二步:在HomeController.cs 中添加如下代码:

public ActionResult Detail(int id)
        {

            //var detail = DBHandle.DBContext.Stat1_a_02s.Single(c => c.ID == id);
            //detail.审核人 = "测试更新";
            //DBHandle.DBContext.SubmitChanges();
            List<SelectListItem> lsSelItem = new List<SelectListItem>();
            SelectListItem sel = new SelectListItem();
            sel.Value = "http://www.baidu.com";
            sel.Text = "百度";
            lsSelItem.Add(sel);
            sel = new SelectListItem();
            sel.Value = "http://www.google.cn";
            sel.Text = "谷歌";
            lsSelItem.Add(sel);
            sel = new SelectListItem();
            sel.Value = "http://www.my400800.cn";
            sel.Text = "400电话";
            lsSelItem.Add(sel);
            sel = new SelectListItem();
            sel.Value = "http://www.qq.com";
            sel.Text = "腾讯";
            lsSelItem.Add(sel);
            SelectList ddlSelData = new SelectList(lsSelItem.AsEnumerable(),"Value","Text", "http://www.my400800.cn");
            ViewData["ddlTestData"] = ddlSelData;
            return View();
        }
 

 

三步:

 

在Views目录下的Home目录下添加Detail.aspx文件,文件内容如下:

 

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Detail
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Detail</h2>
    <%
        using (Html.BeginForm())
        {
    %>
<%--    <%=Html.Encode(Model.审核人) %>
    <%= Html.LabelFor(model => model.审核人)%>--%>
    <%=Html.DropDownList("ddlTest",ViewData["ddlTestData"] as SelectList) %>
    
    <%
        }
    %>
</asp:Content>
 

四步:运行

http://localhost:59765/Home/Detail/1

 

五步:运行结果如下:

 

 

                  Detail

百度 谷歌 400电话 腾讯

 

 

你可能感兴趣的:(html,mvc,腾讯,百度,asp)