太多 select 列表污染了我的控制器操作
Too many select lists polluting my controller actions
我有一个 ASP.Net MVC 应用程序,一些控制器操作有大约 10 个 select 列表(其他有更多)。目前我的代码如下所示:
public ActionResult Edit(int carId)
{
CreateCar model = new CreateCar();
model.Makes = Helper.Makes();
model.Models = Helper.Models();
model.Colors = Helper.Colors();
model.EngineSizes = Helper.EngineSizes();
model.Materials = Helper.Materials();
model.FuelTypes = Helper.FuelTypes();
model.WheelSizes = Helper.WheelSizes();
model.BodyTypes = Helper.BodyTypes();
//more select lists below this
return View(model)
}
在我看来,我设置 select 列表如下:
@Html.DropDownListFor(x => x, Model.Makes)
我的代码在许多操作中看起来像这样,我觉得有更好的方法可以做到这一点,这样我的操作就不会被这些 select 列表污染。
我能想到的解决这个问题的唯一选择是在视图中实际调用助手 class,例如
@Html.DropDownListFor(x => x, Helper.Makes())
这种方法是否被认为是不好的做法,还有其他方法可以解决这个问题吗?
一般来说,是的,在您看来,这样做是不好的做法。控制器负责连接所有东西,所以你的代码应该放在那里。现在,这在很大程度上取决于您的 Helper
class 在做什么。如果 select 列表只是通过一些代码生成的,那么在视图中发生这种情况可能并没有那么糟糕,但是您不想做的是在呈现视图时发出数据库查询.如果您的助手正在与数据库交互,则将其保存在控制器中。
也就是说,这里真正的问题是什么?当然有很多 select 列表,但我不会说这是 "polluting" 你的行为。很清楚你在做什么。该操作负责为您的视图创建模型,这就是它正在做的事情。仅仅因为您的代码中可能有很多行,并不一定意味着它是 "bad" 或 "wrong".
但是,如果您在很多地方重复此操作,我建议将其分解为控制器上的私有或受保护方法。例如:
public ActionResult Edit(int carId)
{
...
PopulateSelectLists(model);
return View(model);
}
[HttpPost]
public ActionResult Edit(Foo model, int carId)
{
...
PopulateSelectLists(model);
return View(model);
}
private void PopulateSelectLists(Foo model)
{
model.Makes = Helper.Makes();
model.Models = Helper.Models();
model.Colors = Helper.Colors();
model.EngineSizes = Helper.EngineSizes();
model.Materials = Helper.Materials();
model.FuelTypes = Helper.FuelTypes();
model.WheelSizes = Helper.WheelSizes();
model.BodyTypes = Helper.BodyTypes();
//more select lists below this
}
然后,一切都干净整洁。
我有一个 ASP.Net MVC 应用程序,一些控制器操作有大约 10 个 select 列表(其他有更多)。目前我的代码如下所示:
public ActionResult Edit(int carId)
{
CreateCar model = new CreateCar();
model.Makes = Helper.Makes();
model.Models = Helper.Models();
model.Colors = Helper.Colors();
model.EngineSizes = Helper.EngineSizes();
model.Materials = Helper.Materials();
model.FuelTypes = Helper.FuelTypes();
model.WheelSizes = Helper.WheelSizes();
model.BodyTypes = Helper.BodyTypes();
//more select lists below this
return View(model)
}
在我看来,我设置 select 列表如下:
@Html.DropDownListFor(x => x, Model.Makes)
我的代码在许多操作中看起来像这样,我觉得有更好的方法可以做到这一点,这样我的操作就不会被这些 select 列表污染。
我能想到的解决这个问题的唯一选择是在视图中实际调用助手 class,例如
@Html.DropDownListFor(x => x, Helper.Makes())
这种方法是否被认为是不好的做法,还有其他方法可以解决这个问题吗?
一般来说,是的,在您看来,这样做是不好的做法。控制器负责连接所有东西,所以你的代码应该放在那里。现在,这在很大程度上取决于您的 Helper
class 在做什么。如果 select 列表只是通过一些代码生成的,那么在视图中发生这种情况可能并没有那么糟糕,但是您不想做的是在呈现视图时发出数据库查询.如果您的助手正在与数据库交互,则将其保存在控制器中。
也就是说,这里真正的问题是什么?当然有很多 select 列表,但我不会说这是 "polluting" 你的行为。很清楚你在做什么。该操作负责为您的视图创建模型,这就是它正在做的事情。仅仅因为您的代码中可能有很多行,并不一定意味着它是 "bad" 或 "wrong".
但是,如果您在很多地方重复此操作,我建议将其分解为控制器上的私有或受保护方法。例如:
public ActionResult Edit(int carId)
{
...
PopulateSelectLists(model);
return View(model);
}
[HttpPost]
public ActionResult Edit(Foo model, int carId)
{
...
PopulateSelectLists(model);
return View(model);
}
private void PopulateSelectLists(Foo model)
{
model.Makes = Helper.Makes();
model.Models = Helper.Models();
model.Colors = Helper.Colors();
model.EngineSizes = Helper.EngineSizes();
model.Materials = Helper.Materials();
model.FuelTypes = Helper.FuelTypes();
model.WheelSizes = Helper.WheelSizes();
model.BodyTypes = Helper.BodyTypes();
//more select lists below this
}
然后,一切都干净整洁。