basic asp.net mvc vc 2013 example

Upload: k54321

Post on 05-Oct-2015

79 views

Category:

Documents


3 download

DESCRIPTION

Basic ASP.net Mvc Vc 2013 Example

TRANSCRIPT

  • P a g e | 0

  • Basic ASP.NET MVC 5 Visual C# 2013

  • Basic ASP.NET MVC 5 Visual C# 2013 : : 12 (397 ) : 2557 : 1st Edition : www.mebmarket.com/ , www.ebooks.in.th : thaivb : ... .. 2537 , , , - https://www.facebook.com/thaivb.net ( 086-022-2299) Visual Studio Microsoft Coporation

  • Web Application ASP.NET MVC 1 ASP.NET ASP.NET MVC , - https://www.facebook.com/thaivb.net 086-022-2299 2557

  • 1 ASP.NET MVC 5 1 1 Microsoft WebMatrix 3 WebMatrix 5 6 7 WebMatrix 9 10 13 WebMatrix 14 WebMatrix 17 17 2 C# Razor 18 18 cshtml 18 21 (Comment) 22 23 for 24 while 25 dowhile 26 foreach 27 28 Layout RenderBody() 29 30

  • (Content File) 31 Layout 35 40 42 3 ASP.NET MVC5 VS 2013 43 43 ASP.NET MVC 5 43 45 ASP.NET MVC5 46 ASP.NET MVC5 49 View() 52 56 4 Controller 57 cshtml Controller 57 Controller cshtml 61 Html.ActionLink() 65 Controller 67 ViewBag 68 71 Html ActionLink() 73 75 2 76 Controller 79 NonAction 87 ActionName 88

  • 92 5 (Model) 93 93 Model 93 Model 93 104 enum 106 130 (Data Collection) 137 144 LINQ to Objects 146 149 151 6 cshtml (View) 152 152 cshtml 152 154 list ViewBag ViewData157 ViewBag 157 ViewData 160 ASP.NET MVC 161 ASP.NET MVC 162 173 FormCollection 177 185

  • 194 (Custom Attribute)197 210 list 212 221 223 7 SQL Server 2014 Express Edition224 224 SQL Server 2014 Express Edition 224 .NET Framework 3.5 Windows 8.1 225 SQL Server 2014 Express Edition 227 Northwind 229 Northwind SQL Server 2014 Express Edition230 232 8 Entity Framework233 233 Entity Framework 233 247 SQL Server 250 ADO.NET Entity Data Model 256 259 267 269 278 DropDownList 281

  • 285 286 9 (CRUD) 287 287 Entity Framework 287 296 301 305 310 314 321 10 jQuery ASP.NET MVC 322 322 jQuery 322 327 jQuery 330 340 342 11 343 343 2 343 353 - (Master-Details) 357 369

  • 372 12 Login OAuth 373 373 Facebook Login 373 Login Google Account 383 384

  • 1 ASP.NET MVC 5

    Web Application PC, Web Application ASP.NET MVC 5 Visual C# 2013 1 Web Application MVC , , ASP.NET MVC ASP.NET MVC Web Application ASP.NET ( Visual C# 2013) MVC 3 Web Application ASP.NET (Event Driven Programming) Save , ASP.NET MVC 3

    Models (M) , , , Generic List

  • Page 2

    Views (V)

    Controllers (C) M V

    C# Razor ASP.NET MVC C# Razor VC# 2013 HTML5 ASP.NET .aspx .cs 2

    1. VC# 2013 Razor VC# 2013 Razor WebMatrix

    2. ASP.NET MVC 5 VC# 2013 Visual Studio 2013 ( VS 2013)

    ASP.NET MVC 5 VS 2013 VS 2013 Visual Studio 2013 Update 2 ( 2013.2 ) http://msdn.microsoft.com/ VS 2013

  • Page 3

    Microsoft WebMatrix Microsoft WebMatrix 1. http://www.asp.net/web-pages WebMatrix

    1-1

    1-1 Microsoft WebMatrix

  • Page 4

    2. WebMatrix.exe Microsoft WebMatrix 1-2

    1-2 Microsoft WebMatrix 3. Microsoft WebMatrix 1-3

    1-3 Microsoft WebMatrix

  • Page 5

    1-4 Microsoft WebMatrix WebMatrix WebMatrix WebMatrix New Empty Site 1-5

    1-5

  • Page 6

    1-6 WebMatrix 1-6 Files - Site > Settings Desktop 1-7

    1-7

  • Page 7

    CSHTML ( *.cshtml) C# Razor HTML5 Create a new file WebMatrix 1-8

    1-8 Index.cshtml Index.cshtml

  • Page 8

    1-9 HTML5 Index.cshtml 1-9 Files Index.cshtml HTML5

  • Page 9

    WebMatrix WebMatrix Open > My Sites 1-10

    1-10 Open My Sites WebMatrix 1 EmptySite1 1-11

  • Page 10

    1-11 EmptySite1 WebMatrix Site > Settings 1-12

  • Page 11

    1-12 1-12 1.0 WebMatrix

  • Page 12

    1-13 2.0.30506.0 1-13 WebMatrix

  • Page 13

    Index.cshtml WebMatrix Index.cshtml Site > Settings WebMatrix Index.cshtml 1-14

  • Page 14

    1-14 Index.cshtml WebMatrix HTML5 C# WebMatrix IE

  • Page 15

    1. WebMatrix 1-15

    1-15 2. Index.cshtml Hello Razor 1-16

    1-16

  • Page 16

    1-16 WebMatrix C# Razor 3.

    4. 1 -

    WebMatrix web.config Index.cshtml

    1-17 web.config

  • Page 17

    WebMatrix WebMatrix WebMatrix WebMatrix

    1-18 WebMatrix ASP.NET MVC 5 VC# 2013 VS 2013

  • Page 18

    2 C# Razor C# Razor *.cshtml cshtml cshtml 2

    HTML5 HTML5

    C# Razor Server

    HTML5 JavaScript, Server

  • Page 19

    Hello Razor!!! *.cshtml @{ } Hello Razor!!!

    C# HTML5 2 1. @ *.cshtml

  • Page 20

    : @DateTime.Now

    2-1 2-1 Now DateTime 2. @{.} *.cshtml @{ string strFullName = " "; } - : @strFullName

  • Page 21

    strFullName - strFullName @ 2-2

    2-2

    string string strFullName = " ";

    bool bool bStatus = true; int int ProductPrice = 275; float

    F float VATRATE = 0.07F;

    decimal float M

    decimal ProductCost = 185.67593M;

  • Page 22

    var *.cshtml @{ // string strFullName = " "; bool bStatus = true; int ProductPrice = 275; float VATRATE = 0.07F; decimal ProductCost = 185.67593M; // var strBookName = " ASP.NET MVC5"; }

    strBookName var ASP.NET MVC5 string (Comment) 2

    C# Razor 2 1. 1 @*

    *@

  • Page 23

    2. @* *@

    *.cshtml Hello C# Razor World!!! @**@ @* ASP.NET MVC 5 *@

    Hello C# Razor World!!! C#

  • Page 24

    for for *.cshtml @for(var i = 1; i

  • Page 25

    while while 0 *.cshtml @{ var i = 1; while (i

  • Page 26

    1. i 5 (i

  • Page 27

    2-5 dowhile 2 i 6 (i++) foreach foreach .NET Framework , , Generic List Index.cshtml @{ var arr = new string[] { "VB 2013", "VC# 2013", "ASP.NET 4.5.1" }; } foreach @foreach (string str in arr) {

  • Page 28

    @str }

    2-6 foreach

    1. string arr 3 VB 2013, VC# 2013 ASP.NET 4.5.1 string

    Index.cshtml @{ var arr = new string[] { "VB 2013", "VC# 2013", "ASP.NET 4.5.1" }; }

    2. arr foreach str string

  • Page 29

    foreach str

    Index.cshtml @foreach (string str in arr)

    3. HTML5 @str

    Index.cshtml { @str }

    Layout RenderBody() ASP.NET MVC Layout 2

    (Layout File) (Contents File)

    _ _Layout.cshtml,

    _AdminPage.cshtml HTML5 HTML5 _Layout.cshtml

  • Page 30

    _Layout.cshtml @{ }

    HTML5 6

    HTML5

    HTML5

    utf-8

  • Page 31

    _Layout.cshtml

    1. _Layout.cshtml 2. _Layout.cshtml

    3. _Layout.cshtml

    (Content File) (Contents File) , , HTML5 , , HTML5 HTML5 Index.cshtml Layout _Layout.cshtml _Layout.cshtml Index.cshtml RenderBody()

    Layout RenderBody() 1. Index.cshtml VC# 2013 2-1 Layout RenderBody() (Index.cshtml) @{ Layout = "~/_Layout.cshtml"; Page.Title = "My WebPage";

  • Page 32

    } Content Index.cshtml

    Index.cshtml Content Index.cshtml Layout _Layout.cshtml 2. Layout _Layout.cshtml VC# 2013 Layout RenderBody() (_Layout.cshtml) @{ } Contents _Layout @RenderBody()

  • Page 33

    _Layout.cshtml HTML5 2

    _Layout.cshtml Contents _Layout

    RenderBody()

  • Page 34

    2-7 2

    Index.cshtml Layout _Layout.cshtml _Layout.cshtml Index.cshtml Contents _Layout

  • Page 35

    2-8 2-1 Layout ASP.NET MVC VS 2013 Layout

    Layout 1. Shared

    2-9

    2-9 Shared 2-9

  • Page 36

    _Layout.cshtml Layout

    VC# 2013 2-2 Layout (\Shared\_Layout.cshtml) @{ } @RenderSection("MainMenu") @RenderPage("/Shared/_Header.cshtml") @RenderBody() @RenderPage("/Shared/_Footer.cshtml")

  • Page 37

    _Header.cshtml

    VC# 2013 2-2 Layout (\Shared\_Header.cshtml) @{ } _Header.cshtml _Footer.cshtml

    VC# 2013 2-2 Layout (\Shared\_Footer.cshtml) @{ } | Thaivb.NET |

  • Page 38

    2. Index.cshtml VC# 2013 2-2 Layout (Index.cshtml) @{ Layout = "/Shared/_Layout.cshtml"; Page.Title = "My WebPage"; } @section MainMenu { Home | Webboard | | About Us } Content Index.cshtml

  • Page 39

    2-10 2-2 2-10 4

    Layout Shared 1. _Header.cshtml

    HTML5

    \Shared\_Header.cshtml @{ } _Header.cshtml

  • Page 40

    2. _Footer.cshtml

    \Shared\_Footer.cshtml @{ } | Thaivb.NET |

    3. Index.cshtml MainMenu

    Index.cshtml @section MainMenu { Home | Webboard | | About Us }

  • Page 41

    _Layout.cshtml Layout 4 4. MainMenu Index.cshtml

    \Shared\_Layout.cshtml @{ } @RenderSection("MainMenu")

    5. Layout _Header.cshtml Index.cshtml RenderBody()

    \Shared\_Layout.cshtml @RenderPage("/Shared/_Header.cshtml")

  • Page 42

    @RenderBody() @RenderPage("/Shared/_Footer.cshtml")

    Layout _Footer.cshtml VC# 2013 Razor ASP.NET MVC