ASP.NET MVC4+mongodb+JqueryeasyUI(简单实现CRUD)
前段时间闲来无事学习了一下利用C#操作mongodb,为了方便以后自己学习,同时以是希望能够帮到正在学习mongodb的朋友..............
对于mongodb的一些基础知识,以及如何在dos下操作,网上已经有很多比较详细的资料,此处就不在多说.
天气炎热,直接上代码了.........
后端代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using MongoDB.Bson; 7 using MongoDB.Driver; 8 using System.Data; 9 using MongoDB.Driver.Builders; 10 11 namespace Mvc4ShowMongoDBDemo.Controllers 12 { 13 public class HomeController : Controller 14 { 15 public ActionResult Index() 16 { 17 ViewBag.Message = "用C#操作MongoDB"; 18 var getData = getdataFun().FindAll();//返回的是一个表: 19 ViewBag.mydbDate = getData; 20 21 return View(); 22 } 23 /// <summary> 24 /// 编辑 25 /// </summary> 26 /// <param name="id"></param> 27 /// <returns></returns> 28 [HttpPost] 29 public string Index(string id) 30 { 31 ObjectId _id = ObjectId.Parse(id); 32 BsonDocument data = null; 33 if (!string.IsNullOrEmpty(_id.ToString())) 34 { 35 36 data = getdataFun().FindOneById(_id); 37 } 38 return data.ToJson(); 39 // return View(); 40 } 41 /// <summary> 42 /// 添加或保存 43 /// </summary> 44 /// <param name="name"></param> 45 /// <param name="age"></param> 46 /// <param name="hobby"></param> 47 /// <returns></returns> 48 [HttpPost] 49 public int AddOrEdit(string id,string name, string age, string hobby) 50 { 51 52 if (id.Length > 0) 53 { 54 55 var query = new QueryDocument { { "_id", new ObjectId(id) } }; 56 //定义更新文档 57 var update = new UpdateDocument { { "$set", new QueryDocument { { "hobby", hobby },{"age",age},{"name",name} } 58 } }; 59 60 getdataFun().Update(query,update); 61 return 1; 62 } 63 else 64 { 65 //新增: 66 BsonDocument doc=new BsonDocument{{"name",name},{"age",age},{"hobby",hobby}}; 67 getdataFun().Insert(doc); 68 //WriteConcernResult result= 69 return 1; 70 } 71 72 } 73 /// <summary> 74 ///删除 75 /// </summary> 76 /// <param name="id"></param> 77 /// <returns></returns> 78 [HttpPost] 79 public int del(string id) 80 { 81 //IMongoQuery query = Query.EQ("_id", new ObjectId(id)); 82 var query = new QueryDocument{{"_id", new ObjectId(id)}}; 83 getdataFun().Remove(query); 84 return 1; 85 } 86 /// <summary> 87 /// 获得mongodb中表的集合 88 /// </summary> 89 /// <returns></returns> 90 public MongoCollection<BsonDocument> getdataFun() 91 { 92 //首先需要引入两个dll文件: 93 //使用之前先开启服务: 94 //数据库连接: 95 string mongoconn = "mongodb://127.0.0.1:27017"; 96 //指定数据库: 97 string database = "mydb"; 98 string dataTable = "mydb";//指定表 99 //创建数据链接 100 MongoServer server = MongoServer.Create(mongoconn); 101 //获取指定数据库,如果没有则会自动创建一个: 102 MongoDatabase db = server.GetDatabase(database); 103 //获取表: 104 MongoCollection collection = db.GetCollection(dataTable); 105 106 var data = db.GetCollection("mydb"); 107 return data; 108 } 109 public ActionResult About() 110 { 111 ViewBag.Message = "你的应用程序说明页。"; 112 113 return View(); 114 } 115 116 public ActionResult Contact() 117 { 118 ViewBag.Message = "你的联系方式页。"; 119 120 return View(); 121 } 122 } 123 124 125 }
前端代码:
1 @{ 2 Layout=null; 3 } 4 5 <html> 6 <head> 7 <title></title> 8 <link href="~/css/easyui.css" rel="stylesheet" /> 9 <link href="~/css/dialog.css" rel="stylesheet" /> 10 <link href="~/css/icon.css" rel="stylesheet" /> 11 <script type="text/javascript" src="~/Scripts/jquery-1.7.1.min.js"></script> 12 <script type="text/javascript" src="~/Scripts/jquery-ui-1.8.20.min.js"></script> 13 <script type="text/javascript" src="~/js/jquery.easyui.min.js"></script> 14 <script type="text/javascript"> 15 $(function () { 16 17 $("#close").click(function () { 18 $("#dlg").dialog("close"); 19 }); 20 21 22 }); 23 //编辑 24 function Edit(id) { 25 26 if(id!=0){ 27 28 $.ajax({ 29 url:"/home/index", 30 type:"post", 31 data:{"id":id}, 32 timeout: 600000, 33 async:false, 34 success:function(data){ 35 var item= data.replace("ObjectId(","").replace(")",""); 36 eval("var demo = " +item); 37 $("#name").val(demo.name); 38 $("#age").val(demo.age); 39 $("#hobby").val(demo.hobby); 40 $("#_id").val(demo._id); 41 42 } 43 }); 44 }else{ 45 46 $("#name").val(""); 47 $("#age").val(""); 48 $("#hobby").val(""); 49 50 } 51 $("#dlg").show(); 52 $("#dlg").dialog(); 53 } 54 //保存: 55 function BtnSave(){ 56 $.ajax({ 57 url:"/home/AddOrEdit", 58 type:"post", 59 data:{"id":$("#_id").val(),"name":$.trim($("#name").val()),"age":$("#age").val(),"hobby":$("#hobby").val()}, 60 async:true, 61 success:function(data){ 62 if(data>0){ 63 alert("保存成功"); 64 location.href = location.href; 65 } 66 } 67 }) 68 } 69 //删除: 70 function Del(id){ 71 $.ajax({ 72 url:"/home/Del", 73 type:"post", 74 data:{"id":id}, 75 async:true, 76 success:function(msg){ 77 if(msg>0){ 78 alert("删除成功"); 79 location.href = location.href; 80 } 81 } 82 }) 83 } 84 </script> 85 <style type="text/css"> 86 .butclass { 87 background-color:blue; cursor:pointer; 88 width:50px; padding-left:3px; 89 border: 1px solid #008094; 90 border-radius: 3px;color:white; 91 } 92 93 </style> 94 </head> 95 <body> 96 <div style="margin-top:10px; font-size:22px; color:blue;" align="center"> 97 C#操作mongodb 98 </div> 99 <table width="650px;" align="center" style=" border-color:blue; margin-top:10px;" border="1" cellpadding="0" cellspacing="0"> 100 <thead align="center"> 101 <tr> 102 <th align="center">姓名</th> 103 <th align="center">年龄</th> 104 <th align="center">爱好</th> 105 <th align="center">操作</th> 106 </tr> 107 </thead> 108 <tbody> 109 110 @foreach (var item in ViewBag.mydbDate) 111 { 112 <tr> 113 <td align="center">@item["name"]</td> 114 <td align="center">@item["age"]</td> 115 <td align="center">@item["hobby"]</td> 116 <td align="center" style="width:240px;"><input type="button" onclick="Edit('@item["_id"]')" class="butclass" value="编 辑" /> 117 <input type="button" class="butclass" onclick="Edit(0)" value="添 加" /> 118 <input type="button" class="butclass" onclick="Del('@item["_id"]')" value="删 除" /> 119 </td> 120 </tr> 121 122 } 123 124 </tbody> 125 </table> 126 127 <div id="dlg" title="操作窗口" style="width:400px;height:200px;padding:10px; display:none;"> 128 129 <form id="frm" action=""> 130 <input type="hidden" id="_id" name="_id" value="" /> 131 <span>姓名:</span> 132 <input type="text" id="name" name="name" value="" /><br /> 133 <span>年龄:</span> 134 <input type="text" id="age" name="age" value="" /><br /> 135 <span>爱好:</span> 136 <input type="text" id="hobby" name="hobby" value="" /><br /> 137 </form> 138 <div style=" margin-top:43px; margin-left:240px;"> 139 <input type="button" class="butclass" id="sub" onclick="BtnSave()" value="保存" /> 140 <input type="button" class="butclass" id="close" value="取消" /> 141 </div> 142 143 </div> 144 145 </body> 146 </html>
浙公网安备 33010602011771号