Asp.NetCore MVC Web 应用
Asp.NetCore MVC 与 普通的MVC 基本一致, 只是代码结构稍有改动
一、创建项目
1.
2.
3. 项目结构
二、 构建数据模型
1. Startup类中配置EF Core MySql (NuGet中下载 MySql.Data.EntityFrameworkCore)
2. 使用自带的依赖注入,注册EF Core MySql
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<MovieContext>(options => options.UseMySQL(Configuration.GetConnectionString("MovieContext"))); services.AddMvc(); }
3. Movie 类:
public class Movie { public int ID { get; set; } [Display(Name = "标题")] [StringLength(10, MinimumLength = 3)] public string Title { get; set; } [Display(Name = "发布时间")] [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } [Display(Name = "类型")] public string Genre { get; set; } [Display(Name = "价格")] public decimal Price { get; set; } }
4. MovieContext 类:
public class MovieContext : DbContext { public MovieContext(DbContextOptions<MovieContext> options) : base(options) { } public DbSet<Movie> Movie { get; set; } }
5 . 控制器:
public class MovieController : Controller { private readonly MovieContext _context; public MovieController(MovieContext context) { _context = context; } public IActionResult Index() { var movies = _context.Movie.ToList(); return View(movies); } public IActionResult Create() { return View(); } //[Bind] 特性是防止过度发布的一种方法。 只应在 [Bind] 特性中包含想要更改的属性 //ValidateAntiForgeryToken 特性用于防止请求伪造, [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("ID,Title,ReleaseDate,Genre,Price")] Movie movie) { if (ModelState.IsValid) { return NotFound(); } try { _context.Add(movie); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return RedirectToAction("Index"); } public IActionResult Edit(int id) { var movie = _context.Movie.Where(i => i.ID == id).FirstOrDefault(); return View(movie); } //[Bind] 特性是防止过度发布的一种方法。 只应在 [Bind] 特性中包含想要更改的属性 //ValidateAntiForgeryToken 特性用于防止请求伪造, [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(int id, [Bind("ID,Title,ReleaseDate,Genre,Price")] Movie movie) { if (id != movie.ID) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(movie); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return RedirectToAction("Index"); } return View(movie); } }
6. 创建视图 Create
@model WebApp_Mvc.Models.Movie @{ ViewData["Title"] = "Create"; } <h2>Create</h2> <form asp-action="Create"> <div class="form-horizontal"> <h4>Movie</h4> <hr /> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <input type="hidden" asp-for="ID" /> <div class="form-group"> <label asp-for="Title" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="Title" class="form-control" /> <span asp-validation-for="Title" class="text-danger"></span> </div> </div> <div class="form-group"> <label asp-for="ReleaseDate" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="ReleaseDate" class="form-control" /> <span asp-validation-for="ReleaseDate" class="text-danger"></span> </div> </div> <div class="form-group"> <label asp-for="Genre" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="Genre" class="form-control" /> <span asp-validation-for="Genre" class="text-danger"></span> </div> </div> <div class="form-group"> <label asp-for="Price" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="Price" class="form-control" /> <span asp-validation-for="Price" class="text-danger"></span> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div> </div> </form> <div> <a asp-action="Index">Back to List</a> </div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
Edit:
@model WebApp_Mvc.Models.Movie @{ ViewData["Title"] = "Edit"; } <h2>Edit</h2> <form asp-action="Edit"> <div class="form-horizontal"> <h4>Movie</h4> <hr /> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <input type="hidden" asp-for="ID" /> <div class="form-group"> <label asp-for="Title" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="Title" class="form-control" /> <span asp-validation-for="Title" class="text-danger"></span> </div> </div> <div class="form-group"> <label asp-for="ReleaseDate" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="ReleaseDate" class="form-control" /> <span asp-validation-for="ReleaseDate" class="text-danger"></span> </div> </div> <div class="form-group"> <label asp-for="Genre" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="Genre" class="form-control" /> <span asp-validation-for="Genre" class="text-danger"></span> </div> </div> <div class="form-group"> <label asp-for="Price" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="Price" class="form-control" /> <span asp-validation-for="Price" class="text-danger"></span> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div> </div> </form> <div> <a asp-action="Index">Back to List</a> </div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
Index:
@model List<WebApp_Mvc.Models.Movie> @{ } <h2>Index</h2> <p> <a asp-action="Create">Create New</a> </p> <table class="table"> <thead> <tr> <th> @Html.DisplayNameFor(model => model[0].Title) </th> <th> @Html.DisplayNameFor(model => model[0].ReleaseDate) </th> <th> @Html.DisplayNameFor(model => model[0].Genre) </th> <th> @Html.DisplayNameFor(model => model[0].Price) </th> <th></th> </tr> </thead> <tbody> @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> <td> <a asp-action="Edit" asp-route-id="@item.ID">Edit</a> | <a asp-action="Details" asp-route-id="@item.ID">Details</a> | <a asp-action="Delete" asp-route-id="@item.ID">Delete</a> </td> </tr> } </tbody> </table>
编译运行: