当用户导航到我的MVC4应用程序中的特定页面/ Home / About时,我将填充一个类。我用数据填充了一个类,并希望在下拉列表中包含该数据。
我的课看起来像这样:(已更新)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
public class WorkSection : List<WorkSection>
{
[Required]
[Display(Name = "WorkSection")]
public int ID { get; set; }
public string Code { get; set; }
public SelectList WorkSections { get; set; }
public WorkSection()
{
// Default Constructor
}
public WorkSection(int id, string code)
{
this.ID = ws_id;
this.Code = code;
}
}
如何获取此填充的类型为WorkSection的列表,并使该数据源成为下拉列表的数据源?我想以串联的方式显示“代码和源”字段,例如下拉列表中的“代码:源”,其中ID为所选项目的值。
更新为ActionResult,将在/ Home / About上调用代码
public ActionResult About()
{
WorkSection model = new WorkSection();
OracleConnection con = new OracleConnection();
con.ConnectionString = "omitted";
try
{
con.Open();
}
catch (Exception ex)
{
throw ex;
}
try
{
OracleDataReader reader = null;
// Work Section
OracleCommand cmd = new OracleCommand("SELECT ID, CODE FROM MyTable ORDER BY CODE", con);
reader = cmd.ExecuteReader();
while (reader.Read())
{
model.Add(new WorkSection()
{
ID = Int16.Parse(reader["ID"].ToString()),
Code = reader["CODE"].ToString()
});
}
model.WorkSections = BuildSelectList(model.WorkSections, m => m.ID, m => m.Code);
con.Close();
con.Dispose();
}
catch (Exception ex)
{
throw ex;
}
return View(model);
}
首先,我们需要一个视图模型来封装该视图的数据:
public class TestViewModel
{
[Required]
[Display(Name = "Work section")]
// This represents the selected ID on the dropdown
public int WorkSectionId { get; set; }
// The dropdown itself
public SelectList WorkSections { get; set; }
// other properties
}
接下来,我们需要一种填充的方法SelectList
。我前一段时间写了一个自定义方法来做到这一点:
private SelectList BuildSelectList<TSource>(IEnumerable<TSource> source,
Expression<Func<TSource, int>> valueKey, Expression<Func<TSource, string>> textKey,
object selectedValue = null)
{
var selectedValueKey = ((MemberExpression)(MemberExpression)valueKey.Body).Member.Name;
var selectedTextKey = ((MemberExpression)(MemberExpression)textKey.Body).Member.Name;
return new SelectList(source, selectedValueKey, selectedTextKey, selectedValue);
}
这将表达式树用于类型安全性,从而确保在编译时而不是运行时发现问题。SelectList
还对文本键使用一个属性,对值键使用一个属性。在您的情况下,这显然会带来问题,因为您想要组合Code
并Source
形成文本键。为了解决这个问题,您将需要在其中创建一个WorkSection
结合了以下两者的新属性:
public string CodeSource
{
get { return this.Code + ":" + this.Source; }
}
这样,您可以使用它SelectList
正常创建。为此,您的操作可能类似于:
public ActionResult Index()
{
var workSections = // ... fetch from database
TestViewModel model = new TestViewModel();
model.WorkSections = BuildSelectList(workSections, m => m.ID, m => m.CodeSource);
return View(model);
}
您可以像这样在视图中使用它:
@Html.DropDownListFor(m => m.WorkSectionId, Model.WorkSections, "--Please Select--")
@Html.ValidationMessageFor(m => m.WorkSectionId)
关于的最后一点说明BuildSelectList
。一般而言,该方法为我节省了很多时间。如此之多,以至于我现在将其定义为基本控制器上的公共方法,然后从中派生所有控制器。但是,如果要执行此操作,则需要使用[NonAction]
属性对其进行标记,以免干扰路由。
每个评论更新
public class BaseController : Controller
{
[NonAction]
public SelectList BuildSelectList<TSource>(IEnumerable<TSource> source,
Expression<Func<TSource, int>> valueKey, Expression<Func<TSource, string>> textKey,
object selectedValue = null)
{
var selectedValueKey = ((MemberExpression)(MemberExpression)valueKey.Body).Member.Name;
var selectedTextKey = ((MemberExpression)(MemberExpression)textKey.Body).Member.Name;
return new SelectList(source, selectedValueKey, selectedTextKey, selectedValue);
}
}
然后,您可以从中派生控制器BaseController
:
public HomeController : BaseController
{
//
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句