数据库
首页 > 数据库> > 简单的MVC与SQL Server Express LocalDB

简单的MVC与SQL Server Express LocalDB

作者:互联网

简单练习:

 

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>

}

 

标签:set,string,get,LocalDB,Express,Server,Html,id,public
来源: https://www.cnblogs.com/zhangsonglin/p/10436554.html