for IELTS Preparation
http://www.ieltsbuddy.com/ielts-practice.html
http://www.world-english.org/ielts_writing.pdf
Sunday, November 25, 2012
Monday, November 19, 2012
HowTo: From the view to the controller in ASP.NET MVC with ModelBinders
With ASP.NET MVC the developer has now full control about the HTML rendering and how the form data will be transmitted to the server. But how can you get the form values on the server side? There are better ways in MVC to do that than Request.Form["..."].
"Bindings"
In this HowTo you can learn how to access the form data in a more elegant way than Request.Form["..."].
In this HowTo you can learn how to access the form data in a more elegant way than Request.Form["..."].
Structure
We have a "BindingController" and in the Model folder a "Person" class and 3 views:
- "CreatePerson" (form to create a person)
- "Result" of the action
- "Index" is the overview page
- "Result" of the action
- "Index" is the overview page
Person Class:
- public class Person
- {
- public Guid Id { get; set; }
- public string Prename { get; set; }
- public string Surname { get; set; }
- public int Age { get; set; }
- }
Form in CreatePerson.aspx:
- <% using (Html.BeginForm()) {%>
- <%= Html.TextBox("Id") %>
- <%= Html.ValidationMessage("Id", "*") %>
Binding: 1. Option – FormCollection:
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult FormCollection(FormCollection collection)
- {
- Person person = new Person();
- person.Prename = collection["Prename"];
- person.Surname = collection["Surname"];
- person.Age = int.Parse(collection["Age"]);
- return View("Result", person);
- }
This option is the simplest possible way to access the form data, but not very elegant and hard to test, because you need to know which keys are in the FormCollection.
Binding: 2. Option- Parameter Matching:
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult ParameterMatching(string Prename, string Surname, int Age)
- {
- Person person = new Person();
- person.Prename = Prename;
- person.Surname = Surname;
- person.Age = Age;
- return View("Result", person);
- }
This option is very handy. The HTTP form values will be mapped to the parameters if the type and name are the same as the transmitted form value. This option is easy to test, but you have to map the parameters to your model manually.
Binding: 3. Option – Default Binding:
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult DefaultBinding(Person person)
- {
- return View("Result", person);
- }
Now we use our own model as parameter type. The default model binder, which is included in the ASP.NET MVC, will map the incomming HTTP form values to the properties of our person, if type and name match.
Binding: 3. Option with addon – Default Binding with Include:
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult DefaultBindingWithInclude([Bind(Include="Prename")] Person person)
- {
- return View("Result", person);
- }
You should think bevor you bind – to secure your applicaiton you can specify which properties will be mapped to the properties of the parameters.
Binding: 3. Option with addon – Default Binding with Exclude:
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult DefaultBindingWithExclude([Bind(Exclude = "Prename")] Person person)
- {
- return View("Result", person);
- }
The same functionality, but this time reversed.
Binding: 3. Option with addon – Default Binding with Prefix:
If you put a prefix in your Html Input controls (because you have multiple "name" fields), than you can specify a prefix.
All these options can be used together and you can map more than one parameter.
Binding: 4. Option – IModelBinder
If the values are getting more complex or you want your own mapping logic than you can use the IModelBinder interface. One example is Fileupload or to get the session user.
Now you just need to implement the IModelBinder interface:
- public class PersonModelBinder : IModelBinder
- {
- public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
- {
- if (controllerContext == null) {
- throw new ArgumentNullException("controllerContext");
- }
- if (bindingContext == null) {
- throw new ArgumentNullException("bindingContext");
- }
- NameValueCollection collection = controllerContext.RequestContext.HttpContext.Request.Form;
- Person returnValue = new Person();
- returnValue.Id = Guid.NewGuid();
- returnValue.Prename = "Modelbinder: " + collection["Prename"];
- returnValue.Surname = "Modelbinder: " + collection["Surname"];
- int age = 0;
- int.TryParse(collection["Age"], out age);
- returnValue.Age = age;
- return returnValue;
- }
- }
You get full access to the form values or other controller properties or route value.
You can register your Modelbinder global in the Global.ascx:
- protected void Application_Start()
- {
- RegisterRoutes(RouteTable.Routes);
- ModelBinders.Binders[typeof(Person)] = new PersonModelBinder(); // Important!
- }
Everytime you have a parameter of type "Person" the "PersonModelBinder" will try to map the values to this object.
Or you can specify it per ActionMethod:
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult PersonModelBinder([ModelBinder(typeof(PersonModelBinder))] Person person)
- {
- return View("Result", person);
- }
Lists/Array Binding:
You can even "bind" arrays / lists. Read Scott Hanselmans post for more information.
Screens:
Monday, November 5, 2012
How MVC Website is executed
The following outlines
how the request moves through ASP.NET, to the controller, and through the view:
1 Request comes in to /Home.
2 IIS determines the request should be handled by ASP.NET.
3 ASP.NET gives all HttpModules a chance to modify the request.
4 The UrlRoutingModule determines that the URL matches a route configured in
the application.
Listing 1.2 Unique addition to the web.config file
Figure 1.4 The starter project comes with a basic
layout and CSS.
Licensed to Gayle M. Noll
10 CHAPTER 1 Getting started with the ASP.NET MVC Framework
5 The UrlRoutingModule gets the appropriate IHttpHandler from the IRoute-
Handler that is used in the matching route (most often, MvcRouteHandler) as
the handler for the request.
6 The MvcRouteHandler constructs and returns MvcHandler.
7 The MvcHandler, which implements IHttpHandler, executes ProcessRequest.
8 The MvcHandler uses IControllerFactory to obtain an instance of IController
using the "controller" to route data from the route {controller}/
{action}/{id}.
9 The HomeController is found, and its Execute method is invoked.
10 The HomeController invokes the Index action.
11 The Index action adds objects to the ViewData dictionary.
12 The HomeController invokes the ActionResult returned from the action,
which renders a view.
13 The Index view in the Views folder displays the objects in ViewData.
14 The view, derived from System.Web.Mvc.ViewPage, executes its Process-
Request method.
15 ASP.NET renders the response to the browser.
how the request moves through ASP.NET, to the controller, and through the view:
1 Request comes in to /Home.
2 IIS determines the request should be handled by ASP.NET.
3 ASP.NET gives all HttpModules a chance to modify the request.
4 The UrlRoutingModule determines that the URL matches a route configured in
the application.
Listing 1.2 Unique addition to the web.config file
Figure 1.4 The starter project comes with a basic
layout and CSS.
Licensed to Gayle M. Noll
10 CHAPTER 1 Getting started with the ASP.NET MVC Framework
5 The UrlRoutingModule gets the appropriate IHttpHandler from the IRoute-
Handler that is used in the matching route (most often, MvcRouteHandler) as
the handler for the request.
6 The MvcRouteHandler constructs and returns MvcHandler.
7 The MvcHandler, which implements IHttpHandler, executes ProcessRequest.
8 The MvcHandler uses IControllerFactory to obtain an instance of IController
using the "controller" to route data from the route {controller}/
{action}/{id}.
9 The HomeController is found, and its Execute method is invoked.
10 The HomeController invokes the Index action.
11 The Index action adds objects to the ViewData dictionary.
12 The HomeController invokes the ActionResult returned from the action,
which renders a view.
13 The Index view in the Views folder displays the objects in ViewData.
14 The view, derived from System.Web.Mvc.ViewPage, executes its Process-
Request method.
15 ASP.NET renders the response to the browser.
Top Ten Internet Marketing DO's & DON'Ts
DO
- Keep domain names short and memorable (if possible), and ensure that your web site address is widely published. Include it on stationery, business cards, brochures and advertisements, along with email contact information for key personnel.
- Use a sig file. This little file goes at the end of every email you send, (except when subscribing to a mailing list). It is considered acceptable to use your sig for advertising purposes, but keep it brief. It should contain your company name, a brief description of your product or service, and the URL of your web site, if any.
- Keep your web site simple and straight-forward. Resist the urge to glitz it up with gobs of bandwidth-gobbling graphics. Ensure that your site is constructed in such a way as to encourage repeat visits. A visitor who has to wait two minutes for a graphic to load is unlikely to ever return. Offer the visitor meaningful content, links to interesting sites, and regular updates.
- Find a way to stimulate interaction with visitors to your web site. An email link is useful for this purpose, and not hard to establish. Consider creating an online feedback form -- this is a simple and effective way to conduct online market research. Informal polls are also useful for this purpose, and free online tools make them easy to create.
- Consider news groups, mailing lists, and blogs important resources for gathering marketing-related information. Announce your product or service in a net.acceptable way and only in those places that welcome such announcements.
- Consider creating a blog to promote your site, asWebLens has done here. The blogging phenomenon is fast becoming the leading edge of the new "social Internet" and blogs offer many marketing advantages over static web sites, including the ability to push content out proactively, instead of waiting for the world to come to them.
- If you can't make the time commitment blogging demands — successful blogs require regular contributions — at the very least consider adding an RSS feed to your web site so visitors can receive updates automatically. Include a link to the feed on your site, and be sure to submit it to the various RSS feed directories.
- Write good copy, scatter relevant keywords throughout, and take the time to learn about the otheroptimization techniques that will help your site rank high in search results.
DON'T
- Violate the terms of service of Google's Adsense program or spam the search engines via black hattechniques like cloaking or keyword stuffing. It's not worth it — the penalties are severe, and can include banishment.
- Announce your web site until it is completed and fully functional. Take the time to identify the web servers, lists, and usenet newsgroups where it is appropriate to announce web sites. Consider encouraging colleagues to embed links to your web site in their web pages.
- Make users register before they can receive information. Research has shown this approach repels many more people than it attracts. If you must require registration, request only essential details such as name and email address. Make sure your privacy policy is clearly stated and prominently posted.
- Broadcast your message indiscriminately, unless you enjoy being flamed. Consider the case of Canter & Siegel, a Phoenix law firm that posted an advertisement for their immigration services to 9,000 Usenet discussion groups in the Spring of 1994. While they claim to have earned $50,000 from this approach, they also incurred the wrath of the Internet community, receiving thousands of flames and losing their Internet access provider.
- Lose sight of the fact that it is the user, not the advertiser, who foots the bill for Internet advertising. Therefore, if you wish to avoid alienating a prospect, it is imperative that you treat this person with respect and courtesy. Don't insult his intelligence or waste her time.
- Make your email messages or news group posts any longer than they need to be. Make your point clearly and concisely. If you are replying to another post, retain only the minimum amount of quoted material necessary to get the point across. You can make a product announcement, depending on the news group or list and on the manner in which it is announced. Discretion and courtesy are the keys.
- Underestimate the speed at which bad news can travel throughout the Internet, or the negative impact this phenomenon may have on your company. Intel made this mistake once, in ignoring the groundswell of customer anger about a flawed Pentium chip. This dissatisfaction originated in a news group and spread throughout the Internet like wildfire, ultimately forcing Intel to announce a recall.
Subscribe to:
Posts (Atom)