Reputation: 101
If a user ignores the form, and goes directly to the webpage, I want today's date to be the default value.
But I feel like it's bad code to copy paste all my lines of code between these two, when the only thing that is changes is the date.
How can I clean up this code?
First Results() all I do is set objdate1.DateStart myself.
In [HttpPost] I get objdate1.DateStart from the form.
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 });
}
Upvotes: 0
Views: 103
Reputation: 101
User RandomStrangler from the C# Freenode gave me the answer:
"Have logic in a separate class, and just call that from both actions."
So I created a separate class called Query's, and do this:
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));
}
A lot neater.
Upvotes: 0
Reputation: 704
Make your parameter nullable and remove the parameterless method.
[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 });
}
Upvotes: 1