日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

总结Movies MVC3教程示例的知识点

發(fā)布時間:2025/4/16 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 总结Movies MVC3教程示例的知识点 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、web.cofnig數(shù)據(jù)庫連接

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

?二、初始化數(shù)據(jù)庫及數(shù)據(jù)

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改進了刪除功能,加上了刪除確認,目的是防止惡意代碼沒有經(jīng)過確認就刪除數(shù)據(jù)

當(dāng)點擊刪除連接時只是返回確認信息:

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>

下面是確認刪除后執(zhí)行的動作:

[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");
}

四、內(nèi)容搜索過濾功能

先來看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類和數(shù)據(jù)庫連接上下文類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;}
}

六、字段規(guī)則確認驗證

public int ID { get; set; }
[Required(ErrorMessage="標(biāo)題必需要填寫")]
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; }

?

總結(jié)

以上是生活随笔為你收集整理的总结Movies MVC3教程示例的知识点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。