MVC learning 2: basic syntax and basic mvc syntax

Source: Internet
Author: User

MVC learning 2: basic syntax and basic mvc syntax
Directory

I. Call of overload Methods

Ii. Data Transmission

3. Generate controls

Iv. display the loading View

V. Strongly typed View

6. Differences between @ Response. Write () and @ Html. Raw ()

7. String input in the view

8. template page

 

I. Call of overload Methods

Call methods of the same name in the same controller, which can be distinguished by HttpGet and HttpPost.

[HttpGet] public void request (int id = 0) {Response. Write ("get request successful ~ ");} [HttpPost] public void request (Models. Classes c = null) {Response. Write (" post request succeeded ~ Value: "+ c. CName +" + c. CImg );}

 

 

Ii. Data Transmission

How does the Controller transmit data to the View:

1. ViewBag 2. ViewData 3. TempData 4. Model

You cannot add duplicate keys to TempData.

Background assignment:

// 1. ViewBag. bag = "ViewBagbag"; // 2. ViewData. Add ("key", "viewdata"); // 3. TempData if (! TempData. keys. contains ("temp") TempData. add ("temp", "tempdata"); // you cannot Add the same key again. // 4. model Models. classes cl = new Models. classes () {CName = "model name"}; // data return View (cl );

 

Foreground value:

ViewBag: @ ViewBag. bag <br/> ViewData: @ ViewData ["key"] <br/> TempData: @ TempData ["temp"] <br/> forced type view: @ Model. CName <br/>

 

The value of the Model type is equivalent to ViewData. Model.

// 4. model ViewData. model = new Models. classes () {CName = "ViewData. model "}; // equivalent to Models. classes cl = new Models. classes () {CName = "model name"}; // data return View (cl );

 

3. Generate controls

The foreground view can be bound to the background ViewData through @ Html. DropDownList to generate a drop-down box control.

Background code:

ViewData. add ("myselect", new List <SelectListItem> () {new SelectListItem () {Text = "Value 2", Value = "zhier"}, new SelectListItem () {Text = "Value 1", Value = "zhiyi "}});

Foreground View:

Generate a drop-down box control: @ Html. DropDownList ("myselect", "value 1 ")

Webpage source code:

<Select id = "myselect" name = "myselect"> <option value = ""> value 1 </option> <option value = "zhier"> value 2 </option> <option value = "zhiyi"> value: 1 </option> </select>

 

Iv. display the loading View

View loading can be divided into default loading views with the same name as the Controller, or display loading the view you specified.

Public ActionResult setData () {return View (); // loads the default setData View with the same name as the ActionResult method. cshtml // return View ("Index"); // displays the loaded Index. cshtml view Note: do not include a suffix. cshtml}

 

V. Strongly typed View

Define the model strong type on the view page, so that the compiler can give only prompts.

@ Model _ 1MVC learning. Models. Classes: @ Model. CName

 

6. Differences between @ Response. Write () and @ Html. Raw ()

Front-end:

Html. raw: @ Html. raw ("Html. raw ") <br/> Response. write: Output here to the top @ {Response. write ("Response. write output ~ ");}

 

Page source code:

Response. Write output ~ <! DOCTYPE html> 

 

7. String input in the view

On the view page, enter the "pure" string method in the Razor Syntax:

1. <text> label 2 .@:

{<Text> string 1 </text >}< br/> string 2 <br/>@{@: String 3}

 

The page source code is not wrapped with any tags

String 1 <br/> string 2 <br/> string 3

 

 

8. template page

Select the use layout or template page when adding a view.

The template page view is used:

{// Title ViewBag. Title = "mby" ;}< h2> I used the template page Here is the comment @ section, which can be used to specify the position of the corresponding template page @ RenderSection ("hhh", required: false) does required need to be filled in all the other parts except @ section to the @ RenderBody () Position on the template page * @ section hhh {I am just a small pitfall ~~~}

 

Template page code (_ Layout. cshtml ):

<! DOCTYPE html> 

 

Note: These articles are only for my learning records. In case of 2B errors, please note. Don't like it, don't spray it!

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.