zoukankan      html  css  js  c++  java
  • 总结Movies MVC3教程示例的知识点

    一、web.cofnig数据库连接

    <add name="MovieDBContext"
    connectionString="Data Source=.;Initial Catalog=Movies;Integrated Security=True"
    providerName="System.Data.SqlClient"/>

     二、初始化数据库及数据

    using System.ComponentModel.DataAnnotations;

    在Model类的Movie.cs里添加引用和下面的初始类

    public class movieInitializer:DropCreateDatabaseIfModelChanges<MovieDBContext>
    {
    protected override void Seed(MovieDBContext context)
    {
    var movies=new List<Movie>
    {
    new Movie{Title="when harry met sally",
    Price=4.6M},
    new Movie{Title="Ghost",
    Price=200.0M},
    };
    movies.ForEach(d=>context.Movies.Add(d));
    }
    }

     然后在Global.asax.cs的protected void Application_Start()里添加如下代码:

    Database.SetInitializer<MovieDBContext>(new MovieInitializer());

     三、添加MoviesControllers

    Details注意ViewResult要改成:ActionReuslt因为我们要加个HttpNotFound而它是不能返回ViewResult对象的

     public ActionResult  Details(int id=0)
    {
    Movie movie = db.Movies.Find(id);
    if (movie == null)
    {
    return HttpNotFound();
    }
    return View(movie);
    }

    象Edit和Delte最好给一个初始值,以防提交的ID是空的

    public ActionResult Edit(int id=2)
    {
    Movie movie = db.Movies.Find(id);
    if (movie == null)
    {
    return HttpNotFound();
    }
    return View(movie);
    }

    Mvc3改进了删除功能,加上了删除确认,目的是防止恶意代码没有经过确认就删除数据

    当点击删除连接时只是返回确认信息:

     public ActionResult Delete(int id)
    {
    Movie movie = db.Movies.Find(id);
    return View(movie);
    }

    再点击确认按钮进行post提交删除

    a)请注意查看View里的Delete.cshtml文件删除源文件:

    @using (Html.BeginForm()) {
    <p>
    <input type="submit" value="Delete" /> |
    @Html.ActionLink("Back to List", "Index")
    </p>

    b)在生成html的确认删除源文件里可以看到自动生成了action="/movies/Delete/1:

    <form action="/movies/Delete/1" method="post">    <p>
    <input type="submit" value="Delete" /> |
    <a href="/movies">Back to List</a>
    </p>
    </form>

    下面是确认删除后执行的动作:

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id=0)
    {
    Movie movie = db.Movies.Find(id);
    if (movie == null)
    {
    return HttpNotFound();
    }
    db.Movies.Remove(movie);
    db.SaveChanges();
    return RedirectToAction("Index");
    }

    四、内容搜索过滤功能

    先来看SearchIndex.cshtml视图页:

     @using (Html.BeginForm("SearchIndex", "Movies", FormMethod.Get))
    {
    <p>Genre:@Html.DropDownList("movieGenre","All")</p>
    <p>Title:@Html.TextBox("SearchString") <input type="submit" value="Filter" /></p>

    }

    下面是Controller:

    public ActionResult SearchIndex(string movieGenre,string searchString)
    {
    var GenreLst = new List<string>();
    var GenreQry = from d in db.Movies orderby d.Genre select d.Genre;

    GenreLst.AddRange(GenreQry.Distinct());
    ViewBag.movieGenre = new SelectList(GenreLst);
    var movies = from m in db.Movies select m;

    if (!String.IsNullOrEmpty(searchString))
    {
    movies = movies.Where(s => s.Title.Contains(searchString));
    }

    if (string.IsNullOrEmpty(movieGenre))
    return View(movies);
    else
    {
    return View(movies.Where(x => x.Genre == movieGenre));
    }
    }

    下面是生成的html页面源代码:

    <form action="/Movies/SearchIndex" method="get">        
    Genre:
    <select id="movieGenre" name="movieGenre">
    <option value="">All</option>
    <option>comedy</option>
    <option>Romanti Comedy</option>
    </select>

    <p>Title:<input id="SearchString" name="SearchString" type="text" value="" /> <input type="submit" value="Filter" /></p>
    </form>

    五、Movie类和数据库连接上下文类MovieDBContext,注意引用using System.Data.Entity;

    using System.Data.Entity;
    public class Movie
    {
    public int ID{get;set;}
    public string Title{get;set;}
    public decimal Price{get;set;}
    }
    public class MovieDBContext:DbContext
    {
    public DbSet<Movie> Movies{get;set;}
    }

    六、字段规则确认验证

    public int ID { get; set; }
    [Required(ErrorMessage="标题必需要填写")]
    public string Title { get; set; }
    [Required(ErrorMessage="Price Required")]
    [Range(1,100,ErrorMessage="Price must be between $1 and $100")]
    [DisplayFormat(DataFormatString="{0:c}")]
    public decimal Price { get; set; }
    学习交流群:364976091
  • 相关阅读:
    [ZOJ 4062][2018ICPC青岛站][Plants vs. Zombies]
    [Wannafly挑战赛28][B msc和mcc][预处理+枚举]
    [codeforces Mail.Ru Cup 2018 Round 1 D][ xor 操作]
    [codeforces round#475 div2 ][C Alternating Sum ]
    [zoj4045][思维+dfs]
    [zoj4046][树状数组求逆序(强化版)]
    费马大定理以及求解a^2+b^2=c^2的奇偶数列法则
    【HDOJ3567】【预处理bfs+映射+康拓展开hash】
    POJ1279 Art Gallery 多边形的核
    第八周 Leetcode 44. Wildcard Matching 水题 (HARD)
  • 原文地址:https://www.cnblogs.com/firstcsharp/p/2268528.html
Copyright © 2011-2022 走看看