MVC创建通用DropdownList
起因
MVC项目中有数据字典表,定义了多个类型,需要给每个类型做下拉菜单。
不可能每个类型,都敲一个代码,需要做成通用
思路
- 利用MVC的部件方式,分别定义Controller,View和Model;
- Model的字段需要有:控件名称(WidgetName),数据字典类型(TypeName),选中的值(SelectedValue),数据集(SelectListItems)。需要根据数据字典类型去数据库表中过滤需要的类型,放入数据集中;
- View为部分页,Layout=null。
实现
1.Controller定义方法
public virtual ActionResult GetDropdownList(string widgetName, string selectedValue, string typeName)
{
DropdownListModel model = new DropdownListModel();
model.SelectedValue = selectedValue;
model.WidgetName = widgetName;
model.TypeName = typeName;
return View(model);
}
2.Model
public class DropdownListModel
{
/// <summary>
///选中值
/// </summary>
public string SelectedValue;
/// <summary>
/// 控件名称
/// </summary>
public string WidgetName;
/// <summary>
/// 值类型
/// </summary>
public string TypeName;
/// <summary>
/// 状态列表SelectListItems
/// </summary>
public IList<SelectListItem> StateSelectListItems
{
get
{
IList<SelectListItem> selectListItems = new List<SelectListItem>();
IEnumerable<AttrInfo> attrList = WMFactory.Attr.FindByConditions(null, f => f.Code == TypeName);
if (attrList.Count() > 0)
{
Guid attrId = attrList.First().Id;
IEnumerable<AttrEnumInfo> enumList = WMFactory.AttrEnum.FindByConditions(null, f => f.AttrId == attrId);
foreach (AttrEnumInfo attrEnum in enumList)
{
SelectListItem selectListItem = new SelectListItem();
selectListItem.Value = attrEnum.AttrValue;
selectListItem.Text = attrEnum.AttrText;
selectListItems.Add(selectListItem);
}
}
return selectListItems;
}
}
}
3.View
@{
Layout = null;
}
@model ZPS.FX.CRM.WebUI.Areas.ManageCenter.Models.Common.ProductTypeDropdownListModel
@if (!string.IsNullOrEmpty(Model.WidgetName))
{
//指定控件名称
@Html.DropDownListFor(m => m.SelectedValue, Model.StateSelectListItems, new { @id = Model.WidgetName, @Name = Model.WidgetName.Replace("_", "."), @class = "form-control" })
}
else
{
//没有指定控件名称
@Html.DropDownListFor(m => m.SelectedValue, Model.StateSelectListItems, new { @class = "form-control" })
}
使用方式
<div class="col-sm-4">
@Html.Action("GetDropdownList", "Common", new { widgetName = "BizDistrict_Type", selectedValue = (Model == null || Model.BizDistrict == null) ? "" : Model.BizDistrict.Type, typeName = "BizDistrictType" })
</div>
MVC创建通用DropdownList的更多相关文章
- 利用反射和泛型把Model对象按行储存进数据库以及按行取出然后转换成Model 类实例 MVC网站通用配置项管理
利用反射和泛型把Model对象按行储存进数据库以及按行取出然后转换成Model 类实例 MVC网站通用配置项管理 2018-3-10 15:18 | 发布:Admin | 分类:代码库 | 评论: ...
- Spring mvc创建的web项目,如何获知其web的项目名称,访问具体的链接地址?
Spring mvc创建的web项目,如何获知其web的项目名称,访问具体的链接地址? 访问URL: http://localhost:8090/firstapp/login 在eclipse集成的 ...
- MVC创建XML,并实现增删改
原文:MVC创建XML,并实现增删改 如果创建如下的XML: <?xml version="1.0" encoding="utf-8" standalon ...
- ASP.NET MVC创建的网站
ASP.NET MVC创建的网站 最近在写一个网站,昨天刚写完,由于要和朋友一起测试,但是他电脑上没有环境,所以希望我在自己电脑上部署一下,让他直接通过浏览器来访问来测试,所以从昨晚到今天上午,通 ...
- 【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)
HTTP is not just for serving up web pages. It's also a powerful platform for building APIs that expo ...
- 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...
- 使用 ASP.NET Core MVC 创建 Web API(五)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 使 ...
- 使用 ASP.NET Core MVC 创建 Web API(二)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...
- 使用 ASP.NET Core MVC 创建 Web API(三)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 十 ...
随机推荐
- java 包中的一款经典的singleton模式
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- GDI+编程说明及小结
原文地址:http://blog.csdn.net/byxdaz/article/details/5972759 GDI+(Graphics Device Interface Plus图形设备接口加) ...
- linux下阅读源代码的工具
说来真是惭愧呀.一直在用VIM 做开发.却不知道VI 里还有这么好使的工具.以前一直都是用: find -type f -print | xargs grep -i **** 在源代码里查找. 原来L ...
- AKKA初体验
关于thread和actor下面这段话讲的很好Experienced developers have learned to be very careful with unrestricted thre ...
- CC2530芯片介绍
CC2530,体积更小,功能全, C8051内核,使用起来跟平时的单片机没什么两样,就是程序复杂一些,需要一些时日才能看懂,吃透就更长了,而且对于CC2530TI公司提供了一套完整的解决方案,有布线图 ...
- 南阳acm奇偶数分离
这道题的特殊要求是要先先限定了测试数据的组数,所以多加一条循环语句.下面是已通过的代码: #include<stdio.h> main() { int n,m,i,j; ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 升级cocoapods到1.2 beta版本的方法
最近写Swfit3.0, 要用到一些框架, 然后就用cocoapods嘛, 结果说要cocoapods1.1.0版本才行, 而自己的是cocoapods1.0.1版本的, 所以就想着升级嘛, 结果就遇 ...
- textarea在光标位置插入文字
最近开发类似计算器界面,需要在textarea中编辑公式,涉及到 在光标位置插入 字符. 效果如下: + - * / 添加文字 // html代码如下: <!doctype html> & ...
- opencv学习笔记-图像对比度、亮度调节
在数学中我们学过线性理论,在图像亮度和对比度调节中同样适用,看下面这个公式: 在图像像素中其中: 参数f(x)表示源图像像素. 参数g(x) 表示输出图像像素. 参数a(需要满足a>0)被称为增 ...