There are 100 monkeys in the valley below, and you are the general of the tigers. In any given attack, 75% of the tigers you send will catch and eat a monkey. How many would you like to send?
This is how many monkeys the tigers killed:
Discussion
I would put a lot of discussion into this sample, but that was done elsewhere. So if you want discussion, go see the Getting Started tutorial on creating a basic form.
The View
The following is what you have in the markup for the view:
There are 100 monkeys in the valley below, and you are the general of the tigers. In any given attack, 75% of the tigers
you send will catch and eat a monkey. How many would you like to send?
< %= Html.ValidationSummary() %>
< % using (Html.BeginForm())
{ %>
< % } %>
This is how many monkeys the tigers killed: < %= ViewData["NumberOfMonkeysEaten"]%>
Controller
The code for doing the calculations and the validation in the controller is as follows:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TigersAssaultTheMonkeys(int? numberOfTigersSent)
{
int numberOfMonkeysEaten = 0;
if (numberOfTigersSent.HasValue)
{
double numberOfPotentialMonkeysEaten = numberOfTigersSent.Value * .75;
if (numberOfPotentialMonkeysEaten > 100)
{
numberOfMonkeysEaten = 100;
}
else if (numberOfPotentialMonkeysEaten > 0)
{
numberOfMonkeysEaten = Convert.ToInt32(Math.Round(numberOfPotentialMonkeysEaten, 0));
}
}
else
{
ViewData.ModelState.AddModelError("NumberOfTigersSent", "Yo, whassup with that tiger count value?");
}
ViewData["NumberOfMonkeysEaten"] = numberOfMonkeysEaten;
return View();
}
