2016-07-05 5 views
0

MVC my controller(HomeController.cs)モデル(ModelVariables)を使用するhttpPost actionResultメソッド(Battle)を使用しています。別のクラス(Intermediary.cs)は、私が何をしたいのか httpPostメソッド内で別のクラスを使用する方法

コントローラ(HomeController.cs):適切に私のhttpPostのActionResult(バトル)内の任意のvoidメソッドを追加し、適切に実行するには、ここに私のコードです

[HttpGet] 
    public ActionResult Index() 
    { 
     ModelVariables model = new ModelVariables() 
     { 

      CheckBoxItems = Repository.CBFetchItems(), 
      CheckBoxItemsMasteries = Repository.CBMasteriesFetchItems(), 
      CheckBoxItemsLevel = Repository.CBLevelFetchItems(), 
      CheckBoxItemsItems = Repository.CBItemsFetchItems(), 
      CheckBoxItemsFioraSLevel = Repository.CBFioraSLevelFetchItems(), 
      CheckBoxItemsRunes = Repository.CBRunesFetchItems(), 
      Inter = new Intermediary() //Here I instantiate other class 
    }; 



     return View("Index", model); 
    } 

[HttpPost] 
    public ActionResult Battle(ModelVariables model) 
    { 
     Inter.InstantiateRunes(model); //hmm doesent seem to work 

     return View("Battle", model); 
    } 

他のクラス(Intermedi ary.cs):

public void InstantiateRunes(ModelVariables model) 
    { 
     var LifeStealQuintCount = model.CheckBoxItemsRunes.Where(x => x.CBIsSelectedRunes).Select(x => x.CBRunesID = "LS").ToList().Count; 
     var LifeStealQuintValue = model.CheckBoxItemsRunes.Where(x => x.CBIsSelectedRunes && x.CBRunesID == "LS").Select(x => x.CBRunesValue).FirstOrDefault(); 
     if (model.CheckBoxItemsRunes != null && LifeStealQuintCount != 0 && LifeStealQuintValue != 0) 
     { 


      ViewBag.runeTest = LifeStealQuintValue * LifeStealQuintCount; //I set the values here, what's wrong? 
     } 
    } 

ビュー(Battle.cshtml):

@ViewBag.runeTest //unable to display due to void method not working 

が要約:ここに私のコードは、エラーが表示されない、まだ私は値を実行するときに旅行していないようです...

答えて

1

ViewBagControllerクラスのプロパティで、Intermediaryクラス(Controllerとの関係はありません)のViewBagの値を設定すると機能しません。

あなたはどのタイプLifeStealQuintValueで示されているが、そのintを想定し(LifeStealQuintCountのように)との乗算の結果は常に

public int? InstantiateRunes(ModelVariables model) 
{ 
    var LifeStealQuintCount = model.CheckBoxItemsRunes.Where(x => x.CBIsSelectedRunes).Select(x => x.CBRunesID = "LS").ToList().Count; 
    var LifeStealQuintValue = model.CheckBoxItemsRunes.Where(x => x.CBIsSelectedRunes && x.CBRunesID == "LS").Select(x => x.CBRunesValue).FirstOrDefault(); 
    if (model.CheckBoxItemsRunes != null && LifeStealQuintCount != 0 && LifeStealQuintValue != 0) 
    { 
     return LifeStealQuintValue * LifeStealQuintCount; //I set the values here, what's wrong? 
    } 
    return null; 
} 

、その後の変化にあなたの方法を変更し、その後、intになりますしていませんあなたのPOSTメソッドを

[HttpPost] 
public ActionResult Battle(ModelVariables model) 
{ 
    ViewBag.runeTest = Inter.InstantiateRunes(model); 
    return View("Battle", model); 
} 
関連する問題