日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

简单的MVC与SQL Server Express LocalDB

發布時間:2023/11/30 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单的MVC与SQL Server Express LocalDB 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 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>

}

?

轉載于:https://www.cnblogs.com/zhangsonglin/p/10436554.html

總結

以上是生活随笔為你收集整理的简单的MVC与SQL Server Express LocalDB的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。