After the first introductory chapter mvc_

After the master database access methods, we look at a few mvc entry procedures whereby familiar with the common work of mvc.

First, create a project that displays a simple page.

  Points: 1, in order to focus, without the use of templates; 2, project type mvc, easy access to a variety of references vs add folders

  Figure (vs2017, other versions similar, the same below):

  New Project

  

  Select the template and type of project

  

  Right controllers folder, add the controller for the project

  

  Select the controller type (election date)

  

  By convention, named "Home"

  

  After completion, the system automatically creates controller code (the HomeController.cs), which default method (operation) is the Index.

  Right-code point of view of the method in return, add the file to view it

  

  Use the default name (and controller in the operation of the same name), remove the cleanest view all options, use

  

  In the ensuing generated files (html files can be understood as previously learned), the use of web knowledge, some simple content, such as:

  

  Figure above, in addition to characters, they are auto-generated content.

  So far, the easiest mvc process is complete. The right of Explorer, you can see the files created

  

  After running you can see the effect

  

  Note: "Home" in the address bar is the controller name, "Index" is a specific operation. They correspond to classes and methods controller files.

  Later when debugging, if the content can not be run in 32-bit environment occurs, you can adjust vs debugger, replaced 64-bit environment. FIG follows:

  

  Specific Location:

  

Second, the view syntax: Razor

  Core Razor syntax: a bunch of html, the switch at any time to write C # mode with the "@" symbol.

  Example 1: output value of 327 + 254

  

  Example 2: Calculation value + 2 + 3 + ... + 1 100

  

  c # html tag can be mixed and writing at any time switch to a @ C #.

  Example 3: 1-20 with unordered list output

  

  Details Razor syntax, see the book P115.

Three, Mvc the input and output:

  1. Output: value passed to the controller view -viewdata, P85

  Example: delivery of content from the background (the controller) to the front desk (view)  

public ActionResult Index () 
        { 
            ViewData [ " D1 " ] = " I ViewData data from the background " ; 
            TempData [ " D1 " ] = " I TempData data from the background " ;
             return View (); 
        }

  running result:

  

  2. Enter: View to the controller by value - a form of knowledge, P102

  Example: a digital input, double output it.

  Controller code:  

public  class the HomeController: the Controller 
    { 
        // the GET: Home 
        public ActionResult Index ( int A = 0 ) // where a = 0 indicates the default value 
        { 
            the ViewData [ " D1 " ] = A;
             return View (); 
        } 
    }

Front Code:  

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{int x = int.Parse(ViewData["d1"].ToString());
            if (x == 0)
            {
                <div> 还没输入值 </div><div> values entered @ (x) </ div>
            {The else
            }
            
                
                <div>它的2倍是@(x * 2)</div>
            }
        }
    </div>
    <div>
        <form action="/home/index">
            <input type="text" name="a" /><input type="submit" />
        </form>
    </div>
</body>
</html>

  The above example is the operation on a view, we can put it on the controller to run, you try.

  For standard mvc structure, this operation should be considered "business logic" process should be placed in the business layer. Here only to demonstrate work view and controller.

Fourth, integrated example

  1, the input number n, the output n-layer on the screen Triangle.

  Controller code:  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index(int n=0)//此处的a=0表示默认值
        {
            if (n == 0)
            {
                ViewData["d1"] = null;
            }
            else
            {
                int[,] a;
                a = new int[n,n];
                for (int i = 0; i < n; i++)
                {
                    a[i, 0] = 1;
                }
                for (int i = 1; i < n; i++)
                {
                    for (int j = 1; j <= i; j++)
                    {
                        a[i, j] = a[i - 1, j - 1] + a[i - 1, j];
                    }
                }
                ViewData["d1"] = a;
            }
            return View();
        }
    }
}

  View code:  

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @if (ViewData["d1"] != null)
        {
            <table style="width: 100%; border-color:black; padding:0px; border-collapse:collapse;" border='1'>
                @{
                    int[,] a = (int[,])ViewData["d1"];
                    for (int i = 0; i < a.GetLength(0); i++)
                    {
                        <tr>
                            @for (int j = 0; j <= i; j++)
                            {
                                <td>@(a[i, j])</td>
                            }
                        </tr>
                    }
                }
            </table>
        }
    </div>
    <div>
        <form action="/home/index">
            <input type="text" name="n" /><input type="submit" />
        </form>
    </div>
</body>
</html>

  2, combined with a database of knowledge, can complete a simple "CRUD" project database.

Guess you like

Origin www.cnblogs.com/wanjinliu/p/11486407.html