如何清理此控制器?

How do I Clean up this Controller?

如果用户忽略表单,直接进入网页,我希望今天的日期为默认值。

但我觉得将我的所有代码行复制粘贴到这两者之间是错误的代码,因为唯一改变的是日期。

如何清理这段代码?

First Results() 我所做的就是自己设置 objdate1.DateStart。
在 [HttpPost] 中,我从表单中得到 objdate1.DateStart。

    public ActionResult Results()
    {
        Date1 objdate1 = new Date1();
        objdate1.DateStart = DateTime.Now;

        var DataContext = new BalanceDataContext();


        DateTime earliestDate = objdate1.DateStart.Value.AddMonths(-13);

        //Tons of Code omitted Here ---------------------------


        ViewBag.Metric = 1;
        ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
        ViewBag.Title2 = "% of Electric Estimated";
        ViewBag.Descript = "Percentage of estimated electric bills per Month.";
        return View(new QueryView { Date2 = totalbills, Date1 = totalEstimated });
    }

    [HttpPost]
    public ActionResult Results(Date1 objdate1)
    {
        var DataContext = new BalanceDataContext();


        DateTime earliestDate = objdate1.DateStart.Value.AddMonths(-13);

        //Exact same Code omitted Here ---------------------------


        ViewBag.Metric = 1;
        ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
        ViewBag.Title2 = "% of Electric Estimated";
        ViewBag.Descript = "Percentage of estimated electric bills per Month.";
        return View(new QueryView { Date2 = totalbills, Date1 = totalEstimated });
    }

使您的参数可为空并删除无参数方法。

[HttpPost]
public ActionResult Results(Date1? objdate1)
{
var DataContext = new BalanceDataContext();

if (!objdate1.HasValue){
    objdate1 = new Date1();
    objdate1.DateStart = DateTime.Now;
}

DateTime earliestDate = objdate1.DateStart.Value.AddMonths(-13);

//Exact same Code omitted Here ---------------------------


ViewBag.Metric = 1;
ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
ViewBag.Title2 = "% of Electric Estimated";
ViewBag.Descript = "Percentage of estimated electric bills per Month.";
return View(new QueryView { Date2 = totalbills, Date1 = totalEstimated });

}

来自 C# Freenode 的用户 RandomStrangler 给了我答案:

"Have logic in a separate class, and just call that from both actions."

所以我创建了一个名为 Query's 的单独 class,然后执行此操作:

    public ActionResult Results()
    {
        Date1 objdate1 = new Date1();
        objdate1.DateStart = DateTime.Now;

        Querys estview = new Querys();

        ViewBag.Metric = 1;
        ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
        ViewBag.Title2 = "% of Electric Estimated";
        ViewBag.Descript = "Percentage of estimated electric bills per Month.";
        return View(estview.estimatedbills(objdate1.DateStart));
    }
    [HttpPost]
    public ActionResult Results(Date1 objdate1)
    {
        Querys estview = new Querys();

        ViewBag.Metric = 1;
        ViewBag.Message = objdate1.DateStart.Value.ToString("yyyy-MMMM-dd");
        ViewBag.Title2 = "% of Electric Estimated";
        ViewBag.Descript = "Percentage of estimated electric bills per Month.";

        return View(estview.estimatedbills(objdate1.DateStart));
    }

整洁多了。