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

时间:2016-10-31 17:23:23

标签: c# asp.net-mvc optimization

我正在使用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)

2 个答案:

答案 0 :(得分:0)

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

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

等...

答案 1 :(得分:-3)

该代码主要是垃圾(它不会编译)和噪音。首先使用一致的capitalization(在一个地方调用属性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")
    });

}

简单的大写规则: 作为一般规则,它是&#34; Sql&#34;,而不是&#34; SQL&#34;。 &#34; allDBtables&#34;应该是&#34; AllDbTables&#34;或&#34; AllDBTables&#34;。 &#34; allfrequencies&#34;应该是#34; AllFrequencies&#34;。 &#34; allsql&#34;应该是&#34; AllSql&#34;。

实际上,习惯上将下拉值放入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();
}