码迷,mamicode.com
首页 > 数据库 > 详细

简单的MVC与SQL Server Express LocalDB

时间:2019-02-26 13:39:03      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:tput   ignore   tle   text   defaults   gis   检索   shared   encode   

  • 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

标签:tput   ignore   tle   text   defaults   gis   检索   shared   encode   

原文地址:https://www.cnblogs.com/zhangsonglin/p/10436554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!