2016-10-27 6 views
0

ユーザーがフォームを無視してウェブページに直接行くと、今日の日付をデフォルト値にします。このコントローラをクリーンアップするにはどうすればいいですか?

しかし私は、これらの2つのコードの間にコードをコピーしてコピーするのは悪いコードだと思っています。

このコードをクリーンアップするにはどうすればよいですか?

最初の結果()はすべて自分で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 }); 
    } 
+1

'objdate1'をとり、ActionResultを返す第3の関数を書いてください。 –

+0

あなたは正確に何を達成しようとしていますか?対応する取得アクションとポストアクションが非常に多くのコードを共有する理由がわからないからです。そのデータを画面に反映するビューモデルも導入する必要があります。なぜ私はビューモデルに2つのフィールドを入れてViewBagに4つのフィールドを持っているのか分かりません。ビューモデルでそれらをすべて保持してみませんか? – Fran

+0

@Fran単純な文字列をビューに送るためには、Viewbagを好むだけです。個人の好み。また、結果については以下を参照してください。私はすべてのコードを別のクラスに入れ、必要なときに呼び出すだけです。 –

答えて

0

ユーザーRandomStranglerは私に答えた: "別のクラス内のロジックを持って、そしてちょうど両方のアクションからということを呼び出す"

だから私は、クエリのと呼ばれる別のクラスを作成し、これを実行します。

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)); 
    } 

ロット滑らかな印象を。

1

パラメータをnullに設定し、パラメータなしのメソッドを削除します。 C#はFreenodeから

[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 }); 

}

+0

パラメータのないメソッドを削除すると、Webページに移動するときに「リソースが見つかりません」というエラーが表示されます。 –

+0

が「httppost」とラベル付けされているためです。それはする必要があります – Stormhashe

関連する問題