• M模式: 类,表示数据的应用程序和使用验证逻辑以强制实施针对这些数据的业务规则。
  • V视图: 应用程序使用动态生成 HTML 响应的模板文件。
  • C控制器: 处理传入的浏览器请求的类中检索模型数据,然后指定将响应返回到浏览器的视图模板。

简单练习:

1、添加Controller

HelloWorldController:

using System.Web;

using System.Web.Mvc;

namespace MvcMovie.Controllers

{

public class HelloWorldController : Controller

{

//

// GET: /HelloWorld/

public string Index()

{

return "This is my <b>default</b> action...";

}

//

// GET: /HelloWorld/Welcome/

public string Welcome()

{

return "This is the Welcome action method...";

}

}

}

设置中的路由的格式应用程序_Start/RouteConfig.cs文件:

格式:/[Controller]/[ActionName]/[Parameters]

 

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(

name: "Default",

url: "{controller}/{action}/{id}",

defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

);

}

带参数的:

public string Welcome(string name, int numTimes = 1) {

return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);

}

参数传递查询字符串

public string Welcome(string name, int ID = 1)

{

return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);

}

在中应用程序_Start\RouteConfig.cs文件中,添加"Hello"路由:

public class RouteConfig

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(

name: "Default",

url: "{controller}/{action}/{id}",

defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

);

routes.MapRoute(

name: "Hello",

url: "{controller}/{action}/{name}/{id}"

);

}

}

2、添加视图

原生样子:

public ActionResult Index()

{

return View();

}

MvcMovie\Views\HelloWorld\Index.cshtml创建文件。

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>@ViewBag.Title - Movie App</title>

@Styles.Render("~/Content/css")

@Scripts.Render("~/bundles/modernizr")

</head>

<body>

<div class="navbar navbar-inverse navbar-fixed-top">

<div class="container">

<div class="navbar-header">

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">

<span class="icon-bar"></span>

<span class="icon-bar"></span>

<span class="icon-bar"></span>

</button>

@Html.ActionLink("MVC Movie", "Index", "Movies", null, new { @class = "navbar-brand" })

</div>

<div class="navbar-collapse collapse">

<ul class="nav navbar-nav">

<li>@Html.ActionLink("Home", "Index", "Home")</li>

<li>@Html.ActionLink("About", "About", "Home")</li>

<li>@Html.ActionLink("Contact", "Contact", "Home")</li>

</ul>

</div>

</div>

</div>

<div class="container body-content">

@RenderBody()

<hr />

<footer>

<p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>

</footer>

</div>

@Scripts.Render("~/bundles/jquery")

@Scripts.Render("~/bundles/bootstrap")

@RenderSection("scripts", required: false)

</body>

</html>

@*@{

Layout = "~/Views/Shared/_Layout.cshtml";

}*@

@{

ViewBag.Title = "Index";

}

<h2>Index</h2>

<p>Hello from our View Template!</p>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>@ViewBag.Title - Movie App</title>

@Styles.Render("~/Content/css")

@Scripts.Render("~/bundles/modernizr")

</head>

HelloWorldController.cs :

using System.Web;

using System.Web.Mvc;

namespace MvcMovie.Controllers

{

public class HelloWorldController : Controller

{

public ActionResult Index()

{

return View();

}

public ActionResult Welcome(string name, int numTimes = 1)

{

ViewBag.Message = "Hello " + name;

ViewBag.NumTimes = numTimes;

return View();

}

}

}

Welcome.cshtml

@{

ViewBag.Title = "Welcome";

}

<h2>Welcome</h2>

<ul>

@for (int i = 0; i < ViewBag.NumTimes; i++)

{

<li>@ViewBag.Message</li>

}

</ul>

3、添加模型

using System;

namespace MvcMovie.Models

{

public class Movie

{

public int ID { get; set; }

public string Title { get; set; }

public DateTime ReleaseDate { get; set; }

public string Genre { get; set; }

public decimal Price { get; set; }

}

}

using System;

using System.Data.Entity;

namespace MvcMovie.Models

{

public class Movie

{

public int ID { get; set; }

public string Title { get; set; }

public DateTime ReleaseDate { get; set; }

public string Genre { get; set; }

public decimal Price { get; set; }

}

public class MovieDBContext : DbContext

{

public DbSet<Movie> Movies { get; set; }

}

}

SQL Server Express LocalDB

Web.config文件:

<add name="MovieDBContext"

connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf"

providerName="System.Data.SqlClient"

/>

<connectionStrings>

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie-fefdc1f0-bd81-4ce9-b712-93a062e01031;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcMovie-fefdc1f0-bd81-4ce9-b712-93a062e01031.mdf" providerName="System.Data.SqlClient" />

<add name="MovieDBContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf" providerName="System.Data.SqlClient" />

</connectionStrings>

using System;

using System.Data.Entity;

namespace MvcMovie.Models

{

public class Movie

{

public int ID { get; set; }

public string Title { get; set; }

public DateTime ReleaseDate { get; set; }

public string Genre { get; set; }

public decimal Price { get; set; }

}

public class MovieDBContext : DbContext

{

public DbSet<Movie> Movies { get; set; }

}

}

public ActionResult Details(int? id)

{

if (id == null)

{

return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

}

Movie movie = db.Movies.Find(id);

if (movie == null)

{

return HttpNotFound();

}

return View(movie);

}

@model MvcMovie.Models.Movie

@{

ViewBag.Title = "Details";

}

<h2>Details</h2>

<div>

<h4>Movie</h4>

<hr />

<dl class="dl-horizontal">

<dt>

@Html.DisplayNameFor(model => model.Title)

</dt>

@*Markup omitted for clarity.*@

</dl>

</div>

<p>

@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |

@Html.ActionLink("Back to List", "Index")

</p>

@foreach (var item in Model) {

<tr>

<td>

@Html.DisplayFor(modelItem => item.Title)

</td>

<td>

@Html.DisplayFor(modelItem => item.ReleaseDate)

</td>

<td>

@Html.DisplayFor(modelItem => item.Genre)

</td>

<td>

@Html.DisplayFor(modelItem => item.Price)

</td>

<th>

@Html.DisplayFor(modelItem => item.Rating)

</th>

<td>

@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |

@Html.ActionLink("Details", "Details", new { id=item.ID })  |

@Html.ActionLink("Delete", "Delete", new { id=item.ID })

</td>

</tr>

}

简单的MVC与SQL Server Express LocalDB的更多相关文章

  1. [C#]SQL Server Express LocalDb(SqlLocalDb)的一些体会

    真觉得自己的知识面还是比较窄,在此之前,居然还不知道SqlLocalDb. SqlLocalDb是啥?其实就是简化SQL Server的本地数据库,可以这样子说,SQL Server既可以作为远程,也 ...

  2. Comparison of SQL Server Compact, SQLite, SQL Server Express and LocalDB

    Information about LocalDB comes from here and SQL Server 2014 Books Online. LocalDB is the full SQL ...

  3. 如何安全的将VMware vCenter Server使用的SQL Server Express数据库平滑升级到完整版

    背景: 由于建设初期使用的vSphere vCenter for Windows版,其中安装自动化过程中会使用SQL Server Express的免费版数据库进行基础环境构建.而此时随着业务量的增加 ...

  4. SQL SERVER EXPRESS 连接字符串

    Microsoft SQL Server Express Edition 为生成应用程序提供了一个简单的数据库解决方案.SQL Server Express Edition 支持完整的 SQL Ser ...

  5. 无法定位 Local Database Runtime 安装。请验证 SQL Server Express 是否正确安装以及本地数据库运行时功能是否已启用。

    错误描述: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provide ...

  6. 使用Visual Studio下自带的SQL Server Express

    软件环境:Windows7(x64) + Visual Studio 2010 + SQL Server Express 2008 1.配置数据库 装VS2010不小心把自带的SQL Server 2 ...

  7. SQL Server与SQL Server Express的区别

    SQL Server Express 2005(以下简称 SQLExpress) 是由微软公司开发的 SQL Server 2005(以下简称 SQL2005)的缩减版,这个版本是免费的,它继承了 S ...

  8. ASP.NET MVC与Sql Server交互,把字典数据插入数据库

    在"ASP.NET MVC与Sql Server交互, 插入数据"中,在Controller中拼接sql语句.比如: _db.InsertData("insert int ...

  9. ASP.NET MVC与Sql Server交互, 插入数据

    在"ASP.NET MVC与Sql Server建立连接"中,与Sql Server建立了连接.本篇实践向Sql Server中插入数据. 在数据库帮助类中增加插入数据的方法. p ...

随机推荐

  1. Robot FrameWork使用中常见问题收集

    1.“假死”现象 在完成一个模块的脚本编写后,多次运行没问题,但是隔了几天再来运行的时候,发现脚本运行会出现浏览器那边不动了,脚本这边的时间一直在跑. 问题原因及解决方法: 问题解决了,原因是**dr ...

  2. Appium基础(四)查找app的appActivity与appPackage

    要查看appActivity需要借助日志:adb logcat>D:/log.log  前提是已经装了Android SDK 在目录D:\Program Files (x86)\android\ ...

  3. spring之文件上传

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. bat cmd 获取管理员权限

    @ echo off % % ver|find "5.">nul&&goto :Admin mshta vbscript:createobject()(win ...

  5. php 文件远程下载

    getFile(“http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg”,”,’xuxin’);/*** php实现下载远程图片保存到本地 ...

  6. JavaScript-判断指定日期是一年中第几天-按照从大到小的顺序输出

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. laravel 之jwt认证使用详解

    转载 http://www.heibaiketang.com/blog/show/3.html https://packagist.org/packages/tymon/jwt-auth#1.0.0- ...

  8. Unity shader学习之遮罩纹理

    什么是遮罩? 遮罩允许我们可以保护某些区域,使它们奂于某些修改. 例如下面的例子,使用遮罩来控制高光反射. 转载请注明出处:http://www.cnblogs.com/jietian331/p/71 ...

  9. META标签之关键词、网页描述设置帮助SEO网站优化(转)

      ASP.NET 4.0 Web Forms针对SEO改进措施中有一个是在Page类中加了2个新属性:MetaKeywords 和MetaDescription,它们使得在后台代码类中用编程的手法设 ...

  10. python pymssql 连接数据库

    1)写在前面 远程连接数据库的时候,端口前面都是用的逗号, 因为惯性思维, 就傻傻的把    ip+,+端口 赋值给server了,然后一直报错, pymssql.InterfaceError: Co ...