Aps.net MVC Primer-run the basic sample

Source: Internet
Author: User
Tags hosting web hosting actionlink

1. Keywords--routing

Configure the path structure of the entire web system, typically executing routeconfig.registerroutes in Global.asax.cs.

     Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }    }
    • URL pattern: ASP. NET will map the name to controller, action, ID;
    • Default root location: The action of the controller,index corresponding to the Home;
    • In such a configuration, direct access to root, Root/home, Root/home/index will point to the same location;

2. Keyword--controller

 namespace   webapplication.controllers{  public  class   Homecontroller:controller { public  
   
     ActionResult Index () {viewbag.greeting  = DateTime.Now.Hour < 
    12 ? 
     " 
    good morning  
     ": 
    "  
    good            Afternoon  
     ;         return   View (); }    }}
   
    • Led by the routing system into the Index under HomeController;
    • Execute the corresponding view file-index.cshtml;
    • In the index method, you can pass values to the view file viewbag.greeting this way;

3. Keywords-view file

@{viewbag.title = "Home page";}<Divclass= "Jumbotron">    <H1>asp</H1>    <Pclass= "Lead">ASP. NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</P>    <P><ahref= "Http://asp.net"class= "btn btn-primary btn-large">Learn More&raquo;</a></P></Div><Divclass= "Row">    <Div>        <P>@ViewBag. Greeting, guest!</P>@Html. ActionLink ("RSVP Now", "Rsvpform"); </Div>    <Divclass= "Col-md-4">        <H2>Getting started</H2>        <P>ASP. NET MVC gives you a powerful, patterns-based-to-build dynamic websites that enables a C        Lean separation of concerns and gives all control over markup for enjoyable, agile development. </P>        <P><aclass= "Btn Btn-default"href= "http://go.microsoft.com/fwlink/?LinkId=301865">Learn More&raquo;</a></P>    </Div>    <Divclass= "Col-md-4">        <H2>Get More Libraries</H2>        <P>NuGet is a free Visual studio extension This makes it easy-to-add, remove, and update libraries and tools in Visual Studio Projects.</P>        <P><aclass= "Btn Btn-default"href= "http://go.microsoft.com/fwlink/?LinkId=301866">Learn More&raquo;</a></P>    </Div>    <Divclass= "Col-md-4">        <H2>Web Hosting</H2>        <P>You can easily find a web hosting company this offers the right mix of features and price for your applications.</P>        <P><aclass= "Btn Btn-default"href= "http://go.microsoft.com/fwlink/?LinkId=301867">Learn More&raquo;</a></P>    </Div></Div>
    • You can use the Razor view engine to interpret the contents of @ "";
    • The viewbag.greeting passed in the Controller played a role in the cshtml;
    • @Html. ActionLink ("RSVP Now", "Rsvpform"); HttpGet to the Rsvpform Action under this controller (Home);

4. HttpGet, HttpPost Properties

 Public classHomecontroller:controller { PublicActionResult Index () {viewbag.greeting= DateTime.Now.Hour < A?"Good Morning":"Good afternoon"; returnView (); } [HttpGet] PublicActionResult Rsvpform () {returnView (); } [HttpPost] PublicActionResult Rsvpform (guestresponse response) {returnView ("Thanks", response); }         PublicActionResult About () {Viewbag.message="Your Application description page."; returnView (); }         PublicActionResult Contact () {Viewbag.message="Your Contact page."; returnView (); }    }
    • The Rsvpform Action request just issued from the view file is triggered by a Get method, so the Rsvpform method with the HttpGet attribute is fired;
    • In the same vein, its view file rsvpform.cshtml will be triggered;

5. Html helper method and model binding

@model WebApplication2.Models.GuestResponse @{viewbag.title = "Rsvpform";}<!DOCTYPE HTML><HTML><Head>    <title>Rsvpform</title></Head><Body>@using (Html.BeginForm ()) {<P>Your name: @Html. textboxfor (x = x.name)</P>         <P>Your Email: @Html. textboxfor (x = x.email)</P>         <P>Your Phone: @Html. textboxfor (x = x.phone)</P>         <P>Would you attend? @Html. dropdownlistfor (x = x.willattend, new []{new SelectListItem () {Text = "Yes, I ' ll be there ', Value = bool. TrueString}, new SelectListItem () {Text = "No, I can ' t come", Value = Bo Ol. FalseString}}, "Choose an option")</P>        <inputtype= "Submit"value= "Submit RSVP" />    }</Body></HTML>
    • The Model keyword strongly binds the view file to the Guestresponse under the model layer Webapplication2.models namespace;
    • Use a lambda expression in the Razor HTML helper to generate an object as an INPUT element of Html (the attributes of the input element are derived from the model);
    • Model binding: When input commits, the Post method with the same name is initiated, and the value of the input element is populated with the model;

6. Use of values after model binding

@model WebApplication2.Models.GuestResponse @{viewbag.title = "Thanks";}<!DOCTYPE HTML><HTML><Head>    <title>Rsvpform</title></Head><Body>    <Div>        <H1>Thank you, @Model. Name</H1>@if (Model.willattend = = True) {@:it is great so you ' re coming, the drinks was already in        The fridge!        } else {@:sorry to hear so can ' t make it, but thanks for letting us know. }    </Div></Body></HTML>
    • Still use Razor to interpret and use the C # model: Model.name;

  

  

Aps.net MVC Primer-run the basic sample

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.