MVC c#对不同的类型执行相同的操作

本文关键字:执行 操作 类型 MVC | 更新日期: 2023-09-27 18:15:46

我正在使用MVC,并希望在[创建]视图中的多个选择框。

我已经成功地做了我想要的,但代码重复,显然我不想:

我的模型:

public class ReportCreateModel
{
    public MultiSelectList allDBtables;
    public MultiSelectList allfrequencies;
    public MultiSelectList allsql;
}

我的控制器:

private Context db = new Context();
public ActionResult Create()
{
    ReportCreateModel rcm = new ReportCreateModel();
    //frequencies
    List<SelectListItem> frequencyItems = new List<SelectListItem>();
    foreach (Frequency f in db.dbFrequency.ToList())
    {
        SelectListItem item = new SelectListItem
        {
            Text = f.frequencyName,
            Value = f.frequencyID.ToString()
        };
        frequencyItems.Add(item);
    }
    //systems
    List<SelectListItem> systemItems = new List<SelectListItem>();
    foreach (DataSystem s in db.dbSystem.ToList())
    {
        SelectListItem item = new SelectListItem
        {
            Text = s.systemName,
            Value = s.systemID.ToString()
        };
        systemItems.Add(item);
    }
    //SQL
    List<SelectListItem> sqlItems = new List<SelectListItem>();
    foreach (SQL s in db.dbSQLUsed.ToList())
    {
        SelectListItem item = new SelectListItem
        {
            Text = s.SQLName + " (" + s.sqltypes.SQLTypeName + ")",
            Value = s.SQLID.ToString()
        };
        sqlItems.Add(item);
    }
    rcm.allfrequencies = new MultiSelectList(frequencyItems, "Value", "Text");
    rcm.allsystems = new MultiSelectList(systemItems, "Value", "Text");
    rcm.allSQL = new MultiSelectList(sqlItems, "Value", "Text");
    return View(rcm);
}

这是根据需要,我可以从MultiSelectLists中创建列表框,但有人可以建议我如何减少代码,这样我就可以只有一个代码块运行指定的数据库实体(dbFrequency,dbSystem,dbSQLUsed)的代码

MVC c#对不同的类型执行相同的操作

我在Robert McKee的解决方案中使用了倒数第二段代码,但是,必须对其进行调整以使其在视图中正确编译和显示:

public ActionResult Create()
{
    ViewBag.AllSystems = new MultiSelectList(db.dbSystem.Select(x=>new { Name=x.systemName, Value=x.systemId }),"Value","Name");
    return View();
}

等等…

这些代码大部分是垃圾(无法编译)和噪音。首先使用一致的大写(在一个地方调用属性allsql,在另一个地方调用allsql)。在一个地方调用属性allsystems,在另一个地方调用属性allDBtables)。那就不要做不必要的事情。

private Context db = new Context();
public ActionResult Create()
{
    return View(new ReportCreateModel
    {
        AllSystems = new MultiSelectList(db.dbSystem,"systemName","systemId"),
        AllFrequencies = new MultiSelctList(db.dbFrequencies, "frequencyName", "frequencyID"),
        AllSql = new MultiSelectList(db.dbSQl.Select(s=>new {NAME=s.SQLName + " (" + s.sqltypes.SQLTypeName + ")",SQLID=s.SQLID}),"NAME","SQLID")
    });
}

如果你想进一步优化你的数据库访问,只要求你真正需要的属性,像这样:

private Context db = new Context();
public ActionResult Create()
{
    return View(new ReportCreateModel
    {
        AllSystems = new MultiSelectList(db.dbSystem.Select(x=>new { Name=x.systemName, Value=s.systemId }),"Name","Value"),
        AllFrequencies = new MultiSelectList(db.dbFrequencies.Select(x=>new { Name=x.frequencyName, Value=x.frequencyID }), "Name", "Value"),
        AllSql = new MultiSelectList(db.dbSQl.Select(s=>new { Name=s.SQLName + " (" + s.sqltypes.SQLTypeName + ")", Value=s.SQLID}),"Name","Value")
    });
}

简单的大写规则:一般来说,它是"Sql",而不是"Sql"。"allDBtables"应该是"allDBtables"或"allDBtables"。"allfrequencies"应该是"allfrequencies"。"allsql"应该是"allsql"

实际上,通常将下拉值放入ViewBag中,而不是放入viewmodel中,在这种情况下,它将是:

public ActionResult Create()
{
    ViewBag.AllSystems = new MultiSelectList(db.dbSystem.Select(x=>new { Name=x.systemName, Value=s.systemId }),"Name","Value");
    ViewBag.AllFrequencies = new MultiSelectList(db.dbFrequencies.Select(x=>new { Name=x.frequencyName, Value=x.frequencyID }), "Name", "Value");
    ViewBag.AllSql = new MultiSelectList(db.dbSQl.Select(s=>new { Name=s.SQLName + " (" + s.sqltypes.SQLTypeName + ")", Value=s.SQLID}),"Name","Value");
    return View();
}

那么作为MultiSelectList是一个UI控件,它真的不应该出现在控制器(关注点分离)。那么它就会变成:

public ActionResult Create()
{
    ViewBag.AllSystems = db.dbSystem.Select(x=>new { Name=x.systemName, Value=s.systemId });
    ViewBag.AllFrequencies = db.dbFrequencies.Select(x=>new { Name=x.frequencyName, Value=x.frequencyID });
    ViewBag.AllSql = db.dbSQl.Select(s=>new { Name=s.SQLName + " (" + s.sqltypes.SQLTypeName + ")", Value=s.SQLID});
    return View();
}