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(, MinimumLength = )]
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>

编译运行:

  

Asp.NetCore MVC Web 应用的更多相关文章

  1. ASP.NETCORE MVC模块化

    ASP.NETCORE MVC模块化编程 前言 记得上一篇博客中跟大家分享的是基于ASP.NETMVC5,实际也就是基于NETFRAMEWORK平台实现的这么一个轻量级插件式框架.那么今天我主要分享的 ...

  2. ASP.NET MVC Web API Post FromBody(Web API 如何正确 Post)

    问题场景: ASP.NET MVC Web API 定义 Post 方法,HttpClient 使用 JsonConvert.SerializeObject 传参进行调用,比如 Web Api 中定义 ...

  3. ASP.NET MVC Web API For APP

    近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过在浏览器中使用 JavaScr ...

  4. [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序

    本文转自:http://www.skcode.cn/archives/281 本文演示ABP框架如何使用AngularJs,ASP.NET MVC,Web API 和EntityFramework构建 ...

  5. 【转载】ASP.NET MVC Web API 学习笔记---联系人增删改查

    本章节简单介绍一下使用ASP.NET MVC Web API 做增删改查.目前很多Http服务还是通过REST或者类似RESP的模型来进行数据操作的.下面我们通过创建一个简单的Web API来管理联系 ...

  6. Asp.net mvc web api 在项目中的实际应用

    Asp.net mvc web api 在项目中的实际应用 前言:以下只是记录本人在项目中的应用,而web api在数据传输方面有多种实现方式,具体可根据实际情况而定! 1:数据传输前的加密,以下用到 ...

  7. ASP.NET MVC Web API 学习笔记---第一个Web API程序

    http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...

  8. 在 ASP.NET MVC Web 应用程序中输出 RSS Feeds

    RSS全称Really Simple Syndication.一些更新频率较高的网站可以通过RSS让订阅者快速获取更新信息.RSS文档需遵守XML规范的,其中必需包含标题.链接.描述信息,还可以包含发 ...

  9. 实战 ASP.NET MVC Web API

    实战 ASP.NET MVC Web API Web API 框架基于 ASP.NET MVC 框架开发,是一个面向 Http 协议的通信框架.相对于 WCF 而言,Web API 只面向于 Http ...

随机推荐

  1. C# 通过二进制,将多个文件合并为一个。

    C# 通过二进制,将多个文件合并为一个. /// <summary> /// 合并文件 /// </summary> /// <param name="strD ...

  2. Media Queries 媒体查询

    1.什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向)为其设定CSS样式,媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于 ...

  3. HashMap,Hash优化与高效散列

    OverView Hash table based implementation of the Map interface. This implementation provides all of t ...

  4. dsm 黑 离线转码 备忘

    6.2以后不行 我用的是 DS3617_6.17-15284 进入下载安装文件和工具 1安装 .套件来源增加 packages.synocommunity.comb.设置信任级别为任何发行者 c.找到 ...

  5. Selection II

    [Selection II] 1.上.下.左.右键可以移动Selection 1个像素.按住Shift键,可以一次移动10个像素. 2.Add Selection模式的快捷键是Shift,Sub Se ...

  6. java链接数据库构建sql语句的时候容易记混的地方

    Connection conn = DBHelper.getconnection(); //封装连接数据库的工具类 String sql = "select * from t_test&qu ...

  7. 查看http的并发请求数及其TCP连接状态

    统计80端口的连接数据 netstat -nat | grep -i "80" | wc -l 统计httpd协议连接数 ps -ef | grep httpd | wc -l 统 ...

  8. vs与qt

    http://blog.csdn.net/woniuye/article/details/54928477 1. #include "qmessagebox.h" QMessage ...

  9. Laravel 上传文件处理

    文件上传 获取上传的文件 可以使用 Illuminate\Http\Request 实例提供的 file 方法或者动态属性来访问上传文件, file 方法返回 Illuminate\Http\Uplo ...

  10. HBase 系列(三)HBase Shell

    HBase 系列(三)HBase Shell ./hbase shell # 进入 hbase 命令行 (1) HBase 命令帮助 help # 查看 HBase 所有的命令 create # 或 ...