最近发现一个 MVC中绑定前台DropDownList ,

并且设置默认选中项的简单方法。

直接上代码

方案一

Action:

 ViewData["goodsTypeList"] = new SelectList(goodsTypeList, "id", "name", goodsTypeId);

1参数,是需要绑定的集合

2参数,DropDownList 的Value

3参数,DropDownList 的Text

4参数,默认值

存放到ViewData["goodsTypeList"]

Html:

@Html.DropDownList("goodsTypeList")

这样默认就绑定了ViewData,并且会自动找到这个goodsTypeList,而且还设定了默认值。真的简单粗暴

方案二

Action

ViewData["goodsTypeList"] = new SelectList(goodsTypeList, "id", "name", goodsTypeId);

Html:

@Html.DropDownListFor(x => x.GoodTypes, (SelectList)ViewData["goodsTypeList"], new { Class = "input_width2 input_normal" })

MVC DropDownList的更多相关文章

  1. C# mvc DropDownList选中状态无效情况分析

    情况: DropDownList控件使用List<SelectListItem>()设置下拉选项和默认值.当控件的Name和后台的ViewBag(或ViewData)的Key重复,会导致选 ...

  2. MVC dropdownlist 后端设置select属性后前端依然不能默认选中的解决方法

    -----------------------------------来自网上的解决方法--------------------------------------------- ASP.Net MV ...

  3. ASP.NET MVC Dropdownlist

    本文介绍如何在网页里显示DropDownList. Step 1: 在Control里面添加方法 public ActionResult ShowDropDownList() { return Vie ...

  4. MVC dropdownlist使用

    View中代码 @{ ViewBag.Title = "dropdownlist"; } <h2>dropdownlist</h2> @using (Htm ...

  5. MVC dropdownlist 下拉框

    List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem() ...

  6. MVC Dropdownlist数据绑定 默认值

    @Html.DropDownList("Data", (SelectList)ViewBag.Data, new { @class = "form-control cho ...

  7. ASP.NET MVC DropdownList的使用

    1:直接使用HTML代码写 <select name="year"> <option value="2011">2010</opt ...

  8. mvc DropDownList默认选项

    DDDContext db = new DDDContext(); List<SelectListItem> selectlistDistrict = new List<Select ...

  9. Devexpress MVC DropDownList (持续更新))

    @Html.DevExpress().DropDownEdit(settings => { settings.Name = "psBankCharge"; settings. ...

随机推荐

  1. SourceInsight工具增强——AStyle(代码格式化)、PC-Lint(静态检查)

    Artistic Style(AStyle) AStyle是一款开源.高效.精简的代码格式化工具,适用于C.C++.C#.Java等.官方地址在:http://astyle.sourceforge.n ...

  2. Luogu3527 POI2011 Meteors 整体二分、树状数组、差分

    传送门 比较板子的整体二分题目,时限有点紧注意常数 整体二分的过程中将时间在\([l,mid]\)之间的流星使用树状数组+差分进行维护,然后对所有国家查看一遍并分好类,递归下去,记得消除答案在\([m ...

  3. ABP+AdminLTE+Bootstrap Table权限管理系统第三节--abp分层体系,实体相关及ABP模块系统

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 ABP模块系统 说了这么久,还没有详细说到abp框架,abp其实基于DDD(领域驱动设计)原则的细看分层如下: 再 ...

  4. C#_图片存取数据库Winform

    #region 用于在PictureBox控件中显示选择的图片        /// <summary>        /// 用于在PictureBox控件中显示选择的图片        ...

  5. CrackMe-005全破详解(图文+源码)--上篇

    逆向破解 | 逆向 | 逆向分析 | CrackMe | Crack | CrackMe5 | CrackMe05 前言 CrackMe005,都说比较变态,很多人给放过去了,但是我还是决定上了它,既 ...

  6. 由一个“两次请求”引出的Web服务器跨域请求访问问题的解决方案

    http://blog.csdn.net/cnhnnyzhy/article/details/53128179 (4)Access-Control-Max-Age 该字段可选,用来指定本次预检请求的有 ...

  7. Nginx基于TCP/UDP端口的四层负载均衡(stream模块)配置梳理

    通过我们会用Nginx的upstream做基于http/https端口的7层负载均衡,由于Nginx老版本不支持tcp协议,所以基于tcp/udp端口的四层负载均衡一般用LVS或Haproxy来做.至 ...

  8. 快速排序 O(nlogn)

    #include<bits/stdc++.h> using namespace std; int a[200],n; void q_sort(int l,int r){ if(l>r ...

  9. pair work结对编程(张艺 杨伊)

    一.结对编程人员: 张艺(学号后三位:185) 杨伊(学号后三位:151) 二.这是我们工作的样子:(图片) 三.结对编程优缺点:  优点:  1.结对编程时间紧密,在一定程度上可以督促双方学习,提高 ...

  10. 2017-2018-2 1723《程序设计与数据结构》第九周作业 & 第二周结对编程 总结

    作业地址 第九次作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1878 (作业界面已评分,可随时查看,如果对自己的评分有意 ...