标签:
1、基架自动生成@Html.EnumDropDownListFor()辅助方法映射到模型类属性的元数据。
@model MajorConstruction.Models.Course
<div class="form-group">
@Html.LabelFor(model => model.CourseType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.CourseType, htmlAttributes: new { @class = "form-control" }) //@Html.EnumDropDownListFor()辅助方法映射到模型类的属性的元数据。
@Html.ValidationMessageFor(model => model.CourseType, "", new { @class = "text-danger" })
</div>
</div>
2、使用 枚举类型作为传递参数,利用模型绑定将字符串类型隐式转化为枚举类型。
控制器:
public ActionResult Index(CourseType? courseType)
{
List<CourseType> courseTypeList = new List<CourseType>();
var courseTypeQuery = from c in _courseService.FindAll()
select c.CourseType;
courseTypeList.AddRange(courseTypeQuery.Distinct());
ViewBag.courseTypeList = new SelectList(courseTypeList, courseType); //返回枚举类型selectlist对象并有默认值。
ViewBag.courseType = courseType;
var courses = _courseService.FindAll();
if (courseType != null)
{
courses = courses.Where(c => c.CourseType == courseType) ; //利用模型绑定将字符串类型隐式转化为枚举类型。
}
ViewBag.CourseCount = courses.Count();
return View(courses.OrderBy(c => c.PriorOrder).ThenBy(c => c.CourseName).ToList());
}
视图:
@using (Html.BeginForm("Index", "Course", FormMethod.Get, new { @class = "form-inline", role = "form" }))
{
<label for="courseType" class="control-label">课程类型:</label>
<div class="form-group">
@Html.DropDownList("courseType", ViewBag.courseTypeList as SelectList, "全部课程", new { @class = "form-control" }) //将枚举类型的select对象,绑定到下拉菜单。并显示枚举值。
</div>
<input type="submit" value="查找" class="btn btn-primary" />
}
3、将枚举类型的值字符串作为传递参数,
控制器:
public ActionResult Index(string courseID, string resourceArrtibute, string searchTitle)
{
List<string> _resourceAttributeList = new List<string>();
_resourceAttributeList.AddRange(Enum.GetNames(typeof(ResourceArrtibute))); //通过Enum.GetNames(typeof(枚举类型))的值转换为字符串,并添加到List 泛型对象。
ViewBag.ResourceArrtibute = new SelectList(_resourceAttributeList, resourceArrtibute); //返回所有枚举类型的值,并设定默认值。
ViewBag.SearchTitle = searchTitle;
List<Course> courses = new List<Course>();
var courseListQuery = from r in _resourceService.FindAll().Include(r =>r.Course)
select r.Course;
courses.AddRange(courseListQuery.Distinct().OrderBy(c =>c.CourseName));
ViewBag.CourseID = new SelectList(courses, "CourseID", "CourseName", courseID);
var resources = _resourceService.FindAll().Include(r => r.Course);
if(!string.IsNullOrEmpty(courseID))
{
resources = resources.Where(r => r.CourseID == courseID);
}
if (!string.IsNullOrEmpty(resourceArrtibute))
{
resources = resources.Where(r => r.ResourceArrtibute.ToString() == resourceArrtibute); //利用枚举类型的值的ToString()方法将枚举实例值转换为等效的字符串形式。
}
if (!string.IsNullOrEmpty(searchTitle))
{
resources = resources.Where(r => r.ResourceName.Contains(searchTitle));
}
return View(resources.ToList());
}
视图:
@using (Html.BeginForm("Index", "Resource", FormMethod.Get, new { @class = "form-inline", role = "form" }))
{
<label for="CourseID" class="control-label">课程名称:</label>
<div class="form-group">
@Html.DropDownList("CourseID",null,"全部课程", new { @class="form-control" })
</div>
<label for="ResourceArrtibute" class="control-label">资源类型:</label>
<div class="form-group">
@Html.DropDownList("ResourceArrtibute", null, "全部类型", htmlAttributes: new { @class = "form-control" }) //将字符串形式的selectlist对象绑定到下拉菜单上。
</div>
<label for="searchTextbox" class="control-label"> 通过资源标题查找:</label>
<div class="form-group">
@Html.TextBox("SearchTitle", ViewBag.SearchTitle as string, new { @class = "form-control" })
</div>
<input type="submit" value="查找" class="btn btn-primary" />
}
从上面的例子可以看出,可以将字符串形式的selectList绑定到下拉菜单上,参数传递的是字符串。也可以将强类型的selectList对象绑定到下拉菜单上。参数传递的是ID值。但综合比较,如果只是为了选择而获得值,字符串形式的绑定显然更有效率。
标签:
原文地址:http://www.cnblogs.com/liuyuanhao/p/4510382.html