Q:

Passing data from view to controler and back to the view

I have a view
@using (Html.BeginForm("ActionMethodName"))
{ 
 @Html.Label("Amount")
 @Html.TextBox("txtAmount")
 <input class="button" id="submit" type="submit" value="Submit" />
@ViewBag.Average
}

And Controller
  public ActionResult ActionMethodName(FormCollection collection)
    {
        string input = collection.Get("txtAmount");
        int numberOfItems = Convert.ToInt32(string.Join(null, System.Text.RegularExpressions.Regex.Split(input, "[^\\d]")));

        //float kwota_rachunku=db.RachunekModel.Select(;

        ViewBag.Average = db.RachunekModel.OrderByDescending(m => m.RachunekID)  // -< typo fixed here!!
                .Take(numberOfItems)
                .Average(m => m.Podatek);
        return View();

    }

And simply i want to get average from user input number of elements and send it back to view.
What I'm doing wrong??

A:

Try this: 
@using (Html.BeginForm("ActionMethodName", "YourControllerName"))
{ 
 @Html.Label("Amount")
 @Html.TextBox("txtAmount")
 <input class="button" id="submit" type="submit" value="Submit" />
 @ViewBag.Average
 @Html.Hidden("Average", ViewBag.Average)
}

And Controller
 public ActionResult ActionMethodName(FormCollection collection)
    {
        string input = collection.Get("txtAmount");
        int numberOfItems = Convert.ToInt32(string.Join(null, System.Text.RegularExpressions.Regex.Split(input, "[^\\d]")));

        //float kwota_rachunku=db.RachunekModel.Select(;

        ViewBag.Average = db.RachunekModel.OrderByDescending(m => m.RachunekID)  // -< typo fixed here!!
                .Take(numberOfItems)
                .Average(m => m.Podatek);
        return View();

    }

