http://www.cnblogs.com/leo_wl/archive/2012/02/10/2345890.html
我们已经创建了一个简单的Asp.Net MVC3网站并成功的连接了MongoDB。但只实现了创建和显示的功能。本回实现了完整的增删改查。
创建的部分,上次的代码中存在一些错误,造成了每个属性都会被创建为单独的一条记录,这并不是我们期待的结果。更改如下:
\Controllers\HomeController.cs
1: [HttpPost]
2: public ActionResult Create(FormCollection collection)
3: {
4: try
5: {
6: var db = GetDB();
7: var doc = new BsonDocument();
8:
9: foreach (var key in collection.AllKeys)
10: {
11: doc.Add(new BsonElement(key, collection[key]));
12: }
13:
14: db["testTable"].Insert(doc);
15: return RedirectToAction("Index");
16: }
17: catch
18: {
19: return View();
20: }
21: }
其实这样写并不安全,会把所有从页面POST过来的内容都作为属性创建并保存,但是简单啦。我们注意力主要集中在MongoDB的使用,安全性并不在考虑范围内。
显示的部分,也做了相应的修改,使其更符合Asp.Net MVC3 Razor引擎的规范:
\Controllers\HomeController.cs
1: public ActionResult Index()
2: {
3: var testTable = GetDB()["testTable"].FindAll();
4: return View(testTable);
5: }
\Views\Home\Index.cshtml
1: @{
2: ViewBag.Title = "Index";
3: Layout = "~/Views/Shared/_Layout.cshtml";
4: }
5: <h2>
6: Index</h2>
7: <ul>
8: @foreach (var testData in Model)
9: {
10: <li>
11: @{
12: var id = string.Empty;
13: foreach (var property in testData.Names)
14: {
15: if (property == "_id")
16: {
17: id = testData[property].ToString();
18: }
19: else
20: {
21: @string.Format("{0}:{1}", property, testData[property]);<br />
22: }
23: }
24: <a href="/Home/Delete/@id">Delete</a> <a href="/Home/Edit/@id">Edit</a> <a href="/Home/Details/@id">Details</a>
25: }
26: </li>
27: }
28: </ul>
29: <a href="/Home/Create">Create New</a>
总体来说,Razor引擎的代码写起来还是流畅+愉快。有一个简单的校技巧,想要链接目录从网站根目录开始的话,要用“/”开头。
显示效果如下:
接下来我们就分别来实现删除,编辑和查看功能。
Details链接:
\Controllers\HomeController.cs
1: public ActionResult Details(string id)
2: {
3: try
4: {
5: var db = GetDB();
6: var doc = db["testTable"].Find(Query.EQ("_id", new BsonObjectId(id))).First();
7: return View(doc);
8: }
9: catch
10: {
11: return View();
12: }
13: }
Edit页面:
\Controllers\HomeController.cs
1: public ActionResult Edit(string id)
2: {
3: var db = GetDB();
4: var doc = db["testTable"].Find(Query.EQ("_id", new BsonObjectId(id))).First();
5: return View(doc);
6: }
\Home\Edit.cshtml
1: @{
2: ViewBag.Title = "Edit";
3: Layout = "~/Views/Shared/_Layout.cshtml";
4: }
5: <h2>
6: Edit</h2>
7: <form method="post" action="/Home/Edit/@Model["_id"].ToString()">
8: Name:
9: <input name="name" type="text" value="@Model["name"]" /><br />
10: Age:
11: <input name="age" type="text" value="@Model["age"]" /><br />
12: Gender:
13: <input name="gender" type="text" value="@Model["gender"]" /><br />
14: Married:
15: <input name="married" type="text" value="@Model["married"]" /><br />
16: <input type="submit" value="Update" />
17: <a href="/Home/Index">Cancel</a>
18: </form>
\Controllers\HomeController.cs
1: [HttpPost]
2: public ActionResult Edit(string id, FormCollection collection)
3: {
4: try
5: {
6: var db = GetDB();
7: var update = new UpdateBuilder();
8: foreach (string key in collection.Keys)
9: {
10: update.Set(key, collection[key]);
11: }
12:
13: db["testTable"].Update(Query.EQ("_id", new BsonObjectId(id)), update);
14: return RedirectToAction("Index");
15: }
16: catch
17: {
18: return View();
19: }
20: }
删除功能:
\Controllers\HomeController.cs
1: public ActionResult Delete(string id)
2: {
3: try
4: {
5: var db = GetDB();
6: db["testTable"].Remove(Query.EQ("_id", new BsonObjectId(id)));
7: return RedirectToAction("Index");
8: }
9: catch
10: {
11: return View();
12: }
13: }
1: routes.MapRoute(
2: "Default", // Route name
3: "{controller}/{action}/{id}", // URL with parameters
4: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
5: );
搞定。
不用设计数据库好爽啊。。想怎么插就怎么插。。。无Schema好爽啊!Razor基本很人性化,缺点很少。
不过感觉MongoDB关方的Connector功力比较弱,不支持Linq也就罢了,implicit也不实现,无法隐式转化格式,写起来很是别扭,不够流畅。
接下来准备研究一下MongoDB的高级功能,敬请期待。
完整代码下载: https://files.cnblogs.com/pandora/MvcApplication1_CRUD.zip