簡單的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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387334.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387334.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387334.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

馬爾可夫鏈 (Markov Chain)是什么鬼

作者&#xff1a;紅猴子鏈接&#xff1a;https://www.zhihu.com/question/26665048/answer/157852228來源&#xff1a;知乎著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載請注明出處。馬爾可夫鏈 &#xff08;Markov Chain&#xff09;是什么鬼 它是隨機…

malloc/free 和 new/delete

(本文參考于網上&#xff09; 首先兩者都可用于申請動態內存和釋放內存&#xff61; 對于非內部數據類型的對象而言&#xff0c;只用malloc/free無法滿足動態對象的要求。對象在創建的同時要自動執行構造函數&#xff0c;對象在消亡之前要自動執行析構函數。由于malloc/free是庫…

主題模型-LDA淺析

個性化推薦、社交網絡、廣告預測等各個領域的workshop上都提到LDA模型&#xff0c;感覺這個模型的應用挺廣泛的&#xff0c;會后抽時間了解了一下LDA&#xff0c;做一下總結&#xff1a; &#xff08;一&#xff09;LDA作用 傳統判斷兩個文檔相似性的方法是通過查看兩個文檔共…

dorado-SplitSpanel控件

1.這是一個界面布局控件 2.分為SideControl邊區域和MainControl主區域 3.常用屬性 3.1 collapsed&#xff1a;打開頁面時&#xff0c;邊區域是否顯示 3.2 position&#xff1a;邊區域占總的大小 轉載于:https://www.cnblogs.com/ergougougou/p/10438752.html

mysql-視圖、事物等

一、視圖 視圖是一個虛擬表&#xff08;非真實存在&#xff09;&#xff0c;其本質是【根據SQL語句獲取動態的數據集&#xff0c;并為其命名】&#xff0c;用戶使用時只需使用【名稱】即可獲取結果集&#xff0c;可以將該結果集當做表來使用。 使用視圖我們可以把查詢過程中的臨…

CAFFE怎樣跑起來

0、參考文獻 [1]caffe官網《Training LeNet on MNIST with Caffe》; [2]薛開宇《讀書筆記4學習搭建自己的網絡MNIST在caffe上進行訓練與學習》&#xff08;[1]的翻譯版&#xff0c;同時還有作者的一些注解&#xff0c;很贊&#xff09;; 1、*.sh文件如何執行&#xff1f; ①方…

運行caffe自帶的兩個簡單例子

為了程序的簡潔&#xff0c;在caffe中是不帶練習數據的&#xff0c;因此需要自己去下載。但在caffe根目錄下的data文件夾里&#xff0c;作者已經為我們編寫好了下載數據的腳本文件&#xff0c;我們只需要聯網&#xff0c;運行這些腳本文件就行了。 注意&#xff1a;在caffe中運…

quartz.net 執行后臺任務

... https://www.cnblogs.com/zhangweizhong/category/771057.html https://www.cnblogs.com/lanxiaoke/category/973331.html 宿主在控制臺程序中 using System;using System.Collections.Specialized;using System.IO;using System.Threading.Tasks;using Quartz;using Quart…

運行caffe自帶的mnist實例詳細教

為了程序的簡潔&#xff0c;在caffe中是不帶練習數據的&#xff0c;因此需要自己去下載。但在caffe根目錄下的data文件夾里&#xff0c;作者已經為我們編寫好了下載數據的腳本文件&#xff0c;我們只需要聯網&#xff0c;運行這些腳本文件就行了。 Mnist介紹&#xff1a;mnist是…

6 軟件的安裝

6 軟件包管理 6.1 簡介 軟件包分類&#xff1a; 源碼包 源代碼&#xff08;大多數是C語言&#xff09; 安裝時慢&#xff0c;容易報錯 >腳本安裝包 對源碼包進行改裝&#xff0c;使安裝更簡單&#xff0c;不多。 rpm包 二進制包 Ubuntu系列的二進制包不是rpm&#xf…

STD函數的內部計算公式

各股票軟件的標準差函數STD是不同的&#xff0c;而布林線的上下軌是以STD為基礎計算出來的&#xff0c;所以使用布林線應小心。以2008/3/28的上證綜指為例&#xff0c;利用如下代碼&#xff1a;"收盤價3日STD:STD(CLOSE,3);"&#xff0c;三日收盤價分別是&#xff1a…

caffe路徑正確,卻讀不到圖片

調試caffe&#xff0c;用已有的網絡訓練自己的數據集的時候&#xff08;我這里做的是二分類&#xff09;。在生成均值文件之后&#xff0c;開始train&#xff0c;發現出現了這個問題。 1&#xff0c;路徑正確&#xff0c;卻讀不到圖片。 [db_lmdb.hpp:15] Check failed: mdb_st…

Eclipse可以執行jsp文件卻無法訪問Tomcat主頁

點擊Servers,然后雙擊本地的Tomcat服務器 出現如下界面 這里要選擇第二項 再重新啟動Tomcat就行了 轉載于:https://www.cnblogs.com/lls1350767625/p/10452565.html

caffe調用的一個例子

本文是學習Caffe官方文檔"ImageNet Tutorial"時做的&#xff0c;同樣由于是Windows版本的原因&#xff0c;很多shell腳本不能直接使用&#xff0c;走了不少彎路&#xff0c;但是收獲也不少。比如&#xff1a;如何讓shell腳本在Windows系統上直接運行、如何去用Caffe給…

孔銅的銅厚

---恢復內容開始--- 表面處理方式注釋&#xff1a; 噴錫 噴錫鉛合金是一種最低成本PCB表面有鉛工藝&#xff0c;它能保持良好的可焊接性。但對于精細引腳間距(<0.64mm)的情況&#xff0c;可能導致焊料的橋接和厚度問題。 無鉛噴錫 一種無鉛表面處理工藝&#xff0c;符合“環…

1 kafka簡介

Publish-subscribe distributed messaging system. A distributed commit log. kafka集群中的服務器都叫broker。 客戶端有兩類&#xff1a;producer、consumer。 客戶端和broker之間使用TCP協議。 不同業務系統的消息通過topic進行區分。 消息的topic會分區&#xff0c;以…

各種機器學習的優缺點及應用場景

目錄 正則化算法&#xff08;Regularization Algorithms&#xff09; 集成算法&#xff08;Ensemble Algorithms&#xff09; 決策樹算法&#xff08;Decision Tree Algorithm&#xff09; 回歸&#xff08;Regression&#xff09; 人工神經網絡&#xff08;Artificial…

微信公眾號接入開發者模式,服務器配置Token驗證

概述 接入微信公眾平臺開發&#xff0c;開發者需要按照如下步驟完成&#xff1a; 填寫服務器配置驗證服務器地址的有效性依據接口文檔實現業務邏輯官方指南文檔服務器配置 服務器地址(URL)&#xff1a;填寫完URL后&#xff0c;微信服務器會發送GET請求&#xff0c;并攜帶以下參…

2 kafka安裝

單節點&#xff1a; 1、解壓kafka壓縮包到安裝目錄&#xff08;自己指定&#xff09;&#xff1b; 2、進入kafka目錄并執行命令 > bin/zookeeper-server-start.sh config/zookeeper.properties #如果報錯&#xff0c;修改kafka-run-class.sh&#xff0c;將 -XX:UseComp…