2013-03-08 51 views
0

使用MVC4,EF5在遗留数据库和代码上进行开发。我们在数据库中有一列(例如productcolor),它有一组不同的值。值可以是“红色”,“绿色”或“蓝色”。数据库列是一个varchar(20)。我无法更改数据库(是的,我读过'枚举' - 但无法触及数据库)。用于DB值的SelectList

我想创建某种通用对象,当表单用于创建/编辑新项目时,总是返回这3个值的选择列表。

现在,我有几个班的[NotMapped]在我的模型

[NotMapped] 
    public string ProductColorSelected { 
     get { return productcolor; } 
     set { productcolor = value; }   
    } 

    [NotMapped] 
    public IEnumerable<SelectListItem> ProductColors { get; set; } 

,然后我手动传递到视图

 product.ProductColors = new[] 
    { 
       new SelectListItem { Value = "Red", Text = "Red" }, 
       new SelectListItem { Value = "Green", Text = "Green" }, 
       new SelectListItem { Value = "Blue", Text = "Blue" }, 
     }; 

与之前创建的控制器中的SelectList查看

@Html.DropDownListFor(model => model.ProductColorSelected , Model.ProductColors) 

这个工程,但我需要在每个控件上创建这个选择列表呃在POST和GET中使用它的类(编辑,创建)。没有真正遵循DRY,但我不确定更好的方法。

此外,如果我有另一个表存储可用于该列的3个值,答案会改变吗?而不是从上面创建选择列表,我会从查找表中获取值(我们在我们的应用程序中有两种情况)?

谢谢!

回答

1

一种可能性是写一个自定义操作过滤器,将每个动作后执行,并填充你的模型ProductColors属性:

public class PopulateProductColorsAttribute: ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     var viewResult = filterContext.Result as ViewResultBase; 
     if (viewResult == null) 
     { 
      // the controller action didn't return a view result => no need to go any further 
      return; 
     } 

     var model = viewResult.Model as SomeModel; 
     if (model == null) 
     { 
      // The controller action didn't pass a model containing the ProductColors property => no need to go any further 
      return; 
     } 

     // now populate the ProductColors property. Of course here you could do a db lookup or whatever 
     model.ProductColors = new[] 
     { 
      new SelectListItem { Value = "Red", Text = "Red" }, 
      new SelectListItem { Value = "Green", Text = "Green" }, 
      new SelectListItem { Value = "Blue", Text = "Blue" }, 
     }; 
    } 
} 

现在所有剩下的就是装修需要这个与所有的控制器动作自定义操作过滤器:

[PopulateProductColors] 
public ActionResult Index() 
{ 
    SomeModel model = ... 
    return View(model); 
} 

[PopulateProductColors] 
[HttpPost] 
public ActionResult Index(SomeModel model) 
{ 
    ... 
} 

或注册为全球行动过滤器在这种情况下,将适用于所有的控制器动作。

+0

很好用!谢谢!!! – 2013-03-09 18:39:52