MVC3 快速构建 影片信息网站; 字符和byte[]的相互转换

(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中) 代码如下

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 MoviesController class 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 @model statement 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 @model statement at the top of the Index.cshtml file:

 

 

  Because the Model object is strongly typed (as an IEnumerable<Movie> object), each item object 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 Movies database 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 );

 

 

     

posted on 2012-07-06 13:59  imihiro  阅读(435)  评论(0编辑  收藏  举报