Good day all, what would be the best way to read a value from a text box in a view and to cast it into an integer in the corresponding controller?
View:
@{
ViewBag.Title = "Index";
}
<h2>Administration</h2>
<p>
Page number:
@Html.TextBox("pageNumber")
@Html.ActionLink("Fetch stats", "FetchStats", "Admin", null,
new { @style = "text-transform:capitalize;" })
</p>
Controller:
public ActionResult FetchStats(int pageNumber)
{
CurrentGameNumber = pageNumber;
[...]
return View("Index");
}
Here’s a simple example. We’ll add a textbox named “PageNumber” to the form within our View to post data to our FetchStats action method. MVC will take care of all of the plumbing, instantiating the controller and calling the
FetchStatsaction method while provide pageNumber as an integer.View
Action Method