2016-07-05 51 views
0

在MVC中我的控制器(HomeController.cs)我有一個使用模型(ModelVariables)的httpPost actionResult方法(Battle),除了當我嘗試從一個void方法不同類(Intermediary.cs),如何在httpPost方法中使用另一個類

我想要做什麼:正確,我httpPost的ActionResult(戰鬥)添加任何空隙的方法和正常運行,這裏是我的代碼:

控制器(HomeController.cs):

[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是)和乘法的結果總是會導致int,然後改變你的方法

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

,然後改變您的POST方法爲

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