ASP Net 000005 | Html | C Sharp (Programming Language)

Please download to get full document.

View again

of 3
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report



Views: 10 | Pages: 3

Extension: TXT | Download: 0

Related documents
ASP Net 000005
  I find that the lambda expression technique prevents me from mistyping the name of the model type property, because VisualStudio IntelliSense pops up and lets me pick the property automatically, as shown in Figure 2-17.48Figure 2-17. Visual Studio IntelliSense for lambda expressions in HTML helper methodsAnother convenient helper method is Html.BeginForm, which generates an HTML form element configured to postback to the action method. Because I have not passed any arguments to the helper method, it assumes I want to post back to thesame URL that the HTML document was requested from. A neat trick is to wrap this in a C# using statement, like this:...@using (Html.BeginForm()) {... form contents go here ...}...Normally, when applied like this, the using statement ensures that an object is disposed of when it goes out of scope. It iscommonly used for database connections, for example, to make sure that they are closed as soon as a query has completed. (Thisapplication of the using keyword is different from the kind that brings classes in a namespace into scope in a class.)Instead of disposing of an object, the HtmlBeginForm helper closes the HTML form element when it goes out ofscope. This means that the Html.BeginForm helper method creates both parts of a form element, like this:<form action= /Home/RsvpForm method= post >... form contents go here ...</form>Don  t worry if you are not familiar with disposing of C# objects. The point here is to demonstrate how to create a form usingthe HTML helper method.Setting the Start URLVisual Studio will, in an effort to be helpful, make the browser request a URL based on the view that is currently being edited. Thisis a hit-and-miss feature because it doesn  t work when you are editing other kinds of file and because you can  t just jump in at anypoint in most complex web apps.To set a fixed URL for the browser to request, select PartyInvites Properties from the Visual StudioProject menu, select the Web section and check the Specific Page option in the Start Action category, asshown in Figure 2-18. You don  t have to enter a value into the field  Visual Studio will request the default URL for the project,which will be directive to the Index action method on the Home controller. (I show you how to use the URL routing systemto change the default mapping in Chapters 15 and 16).49Figure 2-18. Setting the default start URL for the projectYou can see the form in the RsvpForm view when you run the application and click the RSVP Now link. Figure 2-19shows the result.Figure 2-19. The RspvForm viewHandling FormsI have not yet told MVC what I want to do when the form is posted to the server. As things stand, clicking the SubmitRSVP button just clears any values you have entered into the form. That is because the form posts back to the RsvpForm  action method in the Home controller, which just tells MVC to render the view again.50Note You might be surprised that the input data is lost when the view is rendered again. If so, you have probably beendeveloping applications with ASP.NET Web Forms, which automatically preserves data in this situation. I will show you how toachieve the same effect with MVC shortly.To receive and process submitted form data, I am going to use a clever feature. I will add a second RsvpForm actionmethod in order to create the following:A method that responds to HTTP GET requests: A GET request is what a browser issues normally each time someoneclicks a link. This version of the action will be responsible for displaying the initial blank form when someone first visits/Home/RsvpForm.A method that responds to HTTP POST requests: By default, forms rendered using Html.BeginForm() aresubmitted by the browser as a POST request. This version of the action will be responsible for receiving submitted dataand deciding what to do with it.Handing GET and POST requests in separate C# methods helps to keep my controller code tidy, since the two methods havedifferent responsibilities. Both action methods are invoked by the same URL, but MVC makes sure that the appropriate method iscalled, based on whether I am dealing with a GET or POST request. Listing 2-14 shows the changes I applied to theHomeController class.Listing 2-14. Adding an Action Method to Support POST Requests in the HomeController.cs Fileusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using PartyInvites.Models;namespace PartyInvites.Controllers {public class HomeController : Controller {public ViewResult Index() {int hour = DateTime.Now.Hour;ViewBag.Greeting = hour < 12 ? Good Morning : GoodAfternoon ;return View();}[HttpGet]public ViewResult RsvpForm() {return View();}[HttpPost]public ViewResult RsvpForm(GuestResponse guestResponse) {// TODO: Email response to the party organizerreturn View( Thanks , guestResponse);}}}I have added the HttpGet attribute to my existing RsvpForm action method. This tells MVC that this method should beused only for GET requests. I then added an overloaded version of RsvpForm, which takes a GuestResponseparameter and applies the HttpPost attribute. The attribute tells MVC that the n  ew method will deal with POST requests. Ialso imported the PartyInvites.Models namespace  this is just so I can refer to the GuestResponse model51type without needing to qualify the class name. I explain how these additions to the listing work in the following sections.Using Model BindingThe first overload of the RsvpForm action method renders the same view as before  the RsvpForm.cshtml file  togenerate the form shown in Figure 2-18.The second overload is more interesting because of the parameter, but given that the action method will be invoked in responseto an HTTP POST request, and that the GuestResponse type is a C# class, how are the two connected?The answer is model binding, an extremely useful MVC feature whereby incoming data is parsed and the key/value pairs in theHTTP request are used to populate properties of domain model types. This process is the opposite of using the HTML helpermethods; that is, when creating the form data to send to the client, I generated HTML input elements where the values for theid and name attributes were derived from the model class property names.In contrast, with model binding, the names of the input elements are used to set the values of the properties in an instance ofthe model class, which is then passed to the POST-enabled action method.Model binding is a powerful and customizable feature that eliminates the grind and toil of dealing with HTTP requests directlyand lets us work with C# objects rather than dealing with Request.Form[] and Request.QueryString[]values. The GuestResponse object that is passed as the parameter to the action method is automatically populated with thedata from the form fields. I dive into the detail of model binding, including how it can be customized, in Chapter 24.Rendering Other ViewsThe second overload of the RsvpForm action method also demonstrates how to tell MVC to render a specific view in responseto a request, rather than the default view. Here is the relevant statement:...return View( Thanks , guestResponse);...This call to the View method tells MVC to find and render a
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!