0

次の機能が必要なシナリオがあります。

In View I have call as:
$.ajax({
    type: "POST",
    async: false,
    dataType: 'json',
    url: "ControllerA/ActionA",
    data: { var1: some_value },
    success: function (data) {
        if (data == true) {
            form.submit();
        }
        else if (data == false) {
    }
});

// In ControllerA
public JsonResult ActionA(string var1)
{
    /*
 Some manipulation and calculations
 */
 _slist = RedirectToAction("ActionC", "ControllerB", new { var1 = some_value});
 string = _slist.First().ToString();

    return RedirectToAction("ActionB", "ControllerB", new { var1 = var2 });
}

// In ControllerB
public JsonResult ActionB(string var1)
{
    /*
 Some manipulation and calculations
 */

    return Json(false, JsonRequestBehavior.AllowGet);
}

public SelectList ActionC(string var1)
{    
 /*
 Some manipulation and calculations
 */

 Session["STRING"] = some_value;

 return new SelectList(_storeOrderTimeDictionaryList, "Value", "Key");
}

ビューページに JsonResult が必要ですが、問題は次のとおりです。

  1. RedirectToAction が redirecttorouteresult を返すため、JSonResut を直接返すことはできません
  2. ActionC でセッションが必要なため、コントローラーをインスタンス化してアクションを呼び出すことはできません。
4

2 に答える 2

1

これは最善のアプローチではないかもしれません...

わかりにくいですが、コントローラーを枯渇させ、ビジネスロジックを移動すると役立つ場合があります。アクション B と C の機能を維持したいようです。

$.ajax({
    type: "POST",
    async: false,
    dataType: 'json',
    url: "ControllerA/ActionA",
    data: { var1: some_value },
    success: function (data) {
        if (data == true) {
            form.submit();
        }
        else if (data == false) {
    }
});


public Class CalculationsA
{
   public void DoCalculation() {}
}

public Class CalculationsB
{
   public void DoCalculation() {}
}

public Class CalculationsC
{
   public IQueryable<somethign> DoCalculation() {}
}


//_a is declared in Controller A as CalculationsA
//_b is declared in Controller B as CalculationsB 
//_c is declared in Controller C as CalculationsC

// In ControllerA
public JsonResult ActionA(string var1)
{
  _a.DoCalculation(); 
  _slist = _b.DoCalculation().First().ToString();

  Session["STRING"] = some_value;
  _c.DoCalculation();          

  /* your other logic... */

  return Json(retval, JsonRequestBehavior.AllowGet);
}

// In ControllerB
public JsonResult ActionB(string var1)
{
    _b.DoCalculation();

    return Json(false, JsonRequestBehavior.AllowGet);
}

public SelectList ActionC(string var1)
{    
 _c.DoCalculation();

 Session["STRING"] = some_value;

 return new SelectList(_storeOrderTimeDictionaryList, "Value", "Key");
}

ところで、 Ninject、Castle Windsor、Structure Map、またはその他の DI/IOC コンテナーをチェックして、このロジックをテストする (そしてより乾燥させる) のに役立ててください。ninject asp.net mvc 2 チュートリアルを検索してみてください

于 2010-07-26T20:38:02.977 に答える
0

Some manipulation and calculationsコントローラーのアクションをリファクタリングして、別のクラスまたはサービス層の関数呼び出しに抽出することはできませんか。

ActionC でセッションが必要なため、コントローラーをインスタンス化してアクションを呼び出すことはできません。

でのセッションの使用を妨げるものは何もありませんControllerA.ActionA。以下は正確ではありませんが、あなたを助けるかもしれません..

public class ControllerA{
    public JsonResult ActionA(string var1)
    {
     /*  Some manipulation and calculations    */
         SomeService service = new SomeService();
         _slist = service.ActionThatDoesStuffForActionC(var1);
         Session["STRING"] = var1;
         var firstItem = _slist.First().ToString();

         SomeOtherService service2 = new SomeOtherService();
         var service2Result = service2.ActionThatDoesStuffForActionB(firstItem);

         // convert service2Result  to a jsonresult here.

         return RedirectToAction("ActionB", "ControllerB", new { var1 = firstItem });
     }
}

public class ControllerB{
     public JsonResult ActionB(string var1)
     {
          /*    Some manipulation and calculations    */
          SomeOtherService service2 = new SomeOtherService();
          var service2Result = service2.ActionThatDoesStuffForActionB(var1);

          return Json(false, JsonRequestBehavior.AllowGet);
     }

    public SelectList ActionC(string var1)
     {    
     /*     Some manipulation and calculations     */
     SomeService service = new SomeService();
     _slist = service.ActionThatDoesStuffInActionC(var1);
     Session["STRING"] = var1;
     return new SelectList(_slist, "Value", "Key");
    }   
}

編集: ここからソース コードを見てください http://www.lostechies.com/blogs/jimmy_bogard/archive/2010/07/23/mvcconf-slides-and-code-posted.aspx。Jimmy Boggard のアプローチは有用であり、「他のコントローラー」アクションを呼び出す方法を提供すると思います。「アクションの動作を変更できません」とコメントします。そして、リファクタリングには時間がかかりますが、私にはありません。私にとっては、保守不可能なコードへの道のりを示しています。リファクタリング、リファクタリング、リファクタリング - 今それを行うことの利点は、後の段階で何時間もの心痛を省くことができます。そして、質問に基づいて、それはすでに始まっていると思います。

于 2010-07-26T20:41:01.863 に答える