2019-5-10 Asp.net MVC4 DropDownList控件快速Demo

第一步:

创建C#,Mvc4,基本 项目类型.

第二步:

创建Action Index相应的View:

Index.cshtml:

@{
    ViewBag.Title = "Index";
}

<h1>DropDownList Demo</h1>
<h2><a href="#">静态Demo省略</a></h2>
<h2>@Html.ActionLink("Demo1","Demo1")</h2>
<h2>@Html.ActionLink("Demo2","Demo2")</h2>
<h2><hr /></h2>
<h2>@Html.ActionLink("Student","Index","Student")</h2>
<h2>@Html.ActionLink("Demo3","Demo3")</h2>
<h2>@Html.ActionLink("Demo4","Demo4")</h2>

第三步:

在Models目录下,创建Student.cs文件:

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

namespace DropDownListDemo.Models
{
    public class Student
    { 
        public int ID{ set; get; }
        public string Name { set; get; }
       
    }
}

第四步:

添加控制器StudentController.cs,相关选项如图:

点击 [添加] 按钮,产生相应的View文件.

第五步:

打开Models目录下文件:DropDownListDemoContext.cs

复制其中代码:

至Global.asax.cs至Application_Start()事件开头.

第六步:

修改Web.config文件:

第七步:

打开HomeController.cs文件,代码修改如下:

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

namespace DropDownListDemo.Controllers
{
    public class HomeController : Controller
    {
        private DropDownListDemoContext db = new DropDownListDemoContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Demo1()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "Kirin", Value = "1" });
            items.Add(new SelectListItem { Text = "Jade", Value = "2", Selected = true });
            items.Add(new SelectListItem { Text = "Yao", Value = "3" });
            ViewBag.list = items;
            return View();
        }
        public ActionResult Demo2()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "Kirin", Value = "1" });
            items.Add(new SelectListItem { Text = "Jade", Value = "2" });
            items.Add(new SelectListItem { Text = "Yao", Value = "3" });
            ViewBag.list = items;
            ViewBag.selected = 3;
            return View();
        }
        public ActionResult Demo3()
        {
            var students = db.Students;
            var selectList = new SelectList(students, "ID", "Name", "2");
            ViewBag.list = selectList;
            return View();
        }
        public ActionResult Demo4()
        {
            var students = db.Students;
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in students)
            {
                items.Add(new SelectListItem { Text = item.Name, Value = item.ID.ToString() });
            }
            ViewBag.list = items;
            ViewBag.selected = 2;
            return View();
        }
    }
}

第八步:

添加 Demo1、Demo2、Demo3、Demo4 相应的View:

Demo1.cshtml

Demo2.csthml

Demo3.cshtml

Demo4.csthml

源代码与下方的View:Demo1.cshtml类似:

@{
    ViewBag.Title = "Demo1";
}

<h2>Demo1</h2>
@Html.DropDownList("namelist", ViewBag.list as IEnumerable<SelectListItem>)

小结:

  • 前2个demo的下拉列表数据在Action中提前做好.后两个demo的下拉列表数据来自数据中的Student表中.这个Student表中数据可以通过/Student/Index 页面进行插入编辑修改删除等操作.
  • 下拉列表中数据的默认值有两种方式解决

回顾一下DropDownList的三种用法:

  • 建立IEnumerable<SelectListItem>并在其中指定默认选中项.
  • 建立IEnumerable<SelectListItem>,在单独的ViewData项或view model的属性中指定默认选中项.
  • 使用SelectList.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值