ViewBag
or ViewData
Request
object to utilize query strings and form data.cshtml
files"As a new MVC developer I want to learn how to send data from the browser to the server and from the server to the browser, in different ways, so I can see the options I have when creating dynamic web pages with forms and form data."
[Content/Coding] Create a “Home” default landing page that will contain links to each of the other pages you'll create in the tasks below. You must use Razor @Html.ActionLink
HTML helper methods to do this. (Implied in this is that you have an appropriately named controller with action method and a suitable view.)
Below is a basic example. Feel free to reproduce this one or make it your own, but note that yours should look at least as nice and all the routes should be the same, i.e. hostname/Home or just hostname/. This will be explained in the video at the very bottom.
[Content/Coding]Simple Server-Side Dynamic page using HTTP GET Recreate the "Mile to Metric Converter" page and functionality (note the route must be the same, which affects your Controller and View names) shown here:
This is as it appears after the user has typed a number in the Miles input, selected Kilometers and clicked the Convert button. The answer appears below. Note the route.
You must write the form elements yourself using only HTML -- no Razor HTML helpers are allowed for this page. Your controller action method must use GET only and can't have any parameters -- you need to get the form data out of the Request
object.
This page demonstrates that that you can write a simple HTML form, send data to the server as query strings, extract that data within a controller action method by using the Request
object, perform some computation with it, and finally pass data to a view which builds a new page for the user containing the resulting new, procedurally created, content.
Now is a great time to start using the debugging and inspection tools available to you:
Debug.WriteLine
and the debug output window in Visual StudioYou should be able to inspect the page, form values leaving the browser, form values received by the server (localhost in this case) and anything else going on in ASP.NET.
[Content/Coding]Simple Server-Side Dynamic page using HTTP POST Recreate this Color Mixer page:
Here are some requirements for this page:
[HttpPost]
public ActionResult MyPage(int? id, int? size, string kind)
{
//...
}
Color
and ColorTranslator
classes to help with this a bit.merge
your feature branches back into master and push
to your remote repository.Now is the time to make sure you understand precisely what is going on here: from client → HTTP → server → ASP.NET MVC → your code → ASP.NET MVC → server → HTTP → and back to the browser, for both GET and POST. Get this down before we dive deeper into MVC and add database functionality.