ScuddleWuddles
ScuddleWuddles

Reputation: 23

How to use another class inside httpPost method

In MVC my controller (HomeController.cs) I have an httpPost actionResult method (Battle) that uses a model (ModelVariables), everything is fine except when I try including a void method from a different class (Intermediary.cs),

What I want to do: properly add any void method in my httpPost actionresult(Battle) and run properly, here's my code:

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

Other class (Intermediary.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?
        }
    }

View (Battle.cshtml):

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

Summary: My code here shows no errors, yet when I run the values do not seem to travel...

Upvotes: 0

Views: 158

Answers (1)

user3559349
user3559349

Reputation:

ViewBag is a property of Controller class and setting a ViewBag value in your Intermediary class (which has no relationship to Controller) will not work.

You have not indicated what type LifeStealQuintValue is, but assuming its int (as LifeStealQuintCount is) and the the result of the multiplication will always result in int, then change your method to

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

and then change your POST method to

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

Upvotes: 1

Related Questions