MVC3 快速构建 影片信息网站; 字符和byte[]的相互转换
2012-07-06 13:59
288 查看
(1)新建ASP.NET MVC3 Application-->选择Internet Application 模版。 (需要.NET Framework 4.0, 并安装Visual Studio 2010)
(2)在Model目录下添加类Movie.cs。 包含Movie Model和 Movie DbContext 两个类。(需要使用Entity Framework,该框架包含在vs 2010中) 代码如下
(3)右键点击Controller目录-->添加MovieController类 (要求安装了SQL Server Compact (MS的一种免费嵌入式数据库))
选择参数如下:
添加成功后,
多了一个Controller, MovieController.cs. (里面有 CRUD方法)
Views目录下多了名为Movie的目录,下有5个cshtml文件。 是Movie的CRUD操作界面。
现在,这些功能就可完备运行。
各命名空间总结如下:
DbContext EntityFramework.dll, v4.0.30319 DbSet EntityFramework.dll, v4.0.30319
2、 理解MVC添加的代码
MoviesControl.cs
The following line from the
View/Movies/index.cshtml
By including a
Because the
3、运行之后,在App_Data目录下自动生成了 Movies.sdf, 数据库文件
Entity Framework Code First detected that the database connection string that was provided pointed to a
二、字符和byte[]的相互转换
(2)在Model目录下添加类Movie.cs。 包含Movie Model和 Movie DbContext 两个类。(需要使用Entity Framework,该框架包含在vs 2010中) 代码如下
namespace MVC3_Application.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; } } }
(3)右键点击Controller目录-->添加MovieController类 (要求安装了SQL Server Compact (MS的一种免费嵌入式数据库))
选择参数如下:
添加成功后,
多了一个Controller, MovieController.cs. (里面有 CRUD方法)
Views目录下多了名为Movie的目录,下有5个cshtml文件。 是Movie的CRUD操作界面。
现在,这些功能就可完备运行。
各命名空间总结如下:
DbContext EntityFramework.dll, v4.0.30319 DbSet EntityFramework.dll, v4.0.30319
2、 理解MVC添加的代码
MoviesControl.cs
private MovieDBContext db = new MovieDBContext(); // GET: /Movies/ public ViewResult Index() { return View(db.Movies.ToList()); }
The following line from the
MoviesControllerclass instantiates a movie database context, as described previously. You can use the movie database context to query, edit, and delete movies.
privateMovieDBContext db =newMovieDBContext();
View/Movies/index.cshtml
@model IEnumerable<MVC3_Application.Models.Movie>
By including a
@modelstatement at the top of the view template file, you can specify the type of object that the view expects. When you created the movie controller, Visual Web Developer automatically included the following
@modelstatement at the top of the Index.cshtml file:
Because the
Modelobject is strongly typed (as an
IEnumerable<Movie>object), each
itemobject in the loop is typed as
Movie. Among other benefits, this means that you get compile-time checking of the code and full IntelliSense support in the code editor:
@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> @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> }
3、运行之后,在App_Data目录下自动生成了 Movies.sdf, 数据库文件
Entity Framework Code First detected that the database connection string that was provided pointed to a
Moviesdatabase that didn’t exist yet, so Code First created the database automatically. You can verify that it's been created by looking in the App_Data folder. If you don't see the Movies.sdf file, click the Show All Files button in the Solution Explorer toolbar, click the Refresh button, and then expand the App_Data folder.
二、字符和byte[]的相互转换
byte[] byteArray = System.Text.Encoding.Default.GetBytes( str ); 怎么样,够简单吧? 反过来也是一样,把byte[]转成string: string str = System.Text.Encoding.Default.GetString( byteArray );
相关文章推荐
- Java中字符和byte数组之间的相互转换
- Java中字符和byte数组之间的相互转换
- 信息管理系统 网站后台管理快速构建
- Java中字符和byte数组之间的相互转换
- 整型int和字符数组byte相互转换的源程序
- 【MVC】快速构建一个图片浏览网站
- 【MVC】快速构建一个图片浏览网站
- 收藏两个很有用的网站,转换本地化错误信息为English
- java中字符数组和字符串的相互转换
- 第17章 ASP.NET MVC实战: 构建NuGet.org网站
- golang中struct和[]byte的相互转换示例
- java int与byte[]数组的相互转换 补码
- byte[]到short、int、long的相互转换
- byte数组和Int之间的相互转换
- mysql日期和字符相互转换方法
- mysql日期和字符相互转换方法
- C# string类型和byte[]类型相互转换
- java中byte数组与十六进制字符串相互转换
- C# Stream 和 byte[] 相互转换
- Java基本类型与byte数组之间相互转换