2016-07-05 15 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); 
} 
相关问题