MongoDBѧϰ±Ê¼Ç(Èý) ÔÚMVCģʽÏÂͨ¹ýJqgrid±í¸ñ²Ù×÷MongoDBÊý¾Ý
¡¡¡¡
¡¡¡¡ÏÂÃæÎÒÃǽ«Öð²½½²½âÔõôÔÚMVCģʽϽ«MongoDBÊý¾Ý¶ÁÈ¡£¬²¢Õ¹Ê¾ÔÚǰ̨Jqgrid±í¸ñÉÏ¡£Õâ¸ö¡°¼òÒ×ϵͳ¡±µÄ»ù±¾Éè¼Æ˼ÏëÊÇÕâÑùµÄ£º ÎÒÃÇÔÚÊÓͼ²ãչʾ±í¸ñ£¬JqgridÏà¹ØJsÂ߼ȫ²¿·ÅÔÚÒ»¸öJsÎļþÖУ¬¿ØÖƲãʵÏÖÁË¡°Ôöɾ²é¸Ä¡±ËĸöÒµÎñ£¬MongoDBµÄ»ù±¾Êý¾Ý·ÃÎÊ·ÅÔÚÁËÄ£ÐͲã ʵÏÖ¡£ÏÂÃæÎÒÃÇÒ»²½²½ÊµÏÖ¡£
Ò»¡¢ÊµÏÖÊÓͼ²ãJqgrid±í¸ñÂß¼
¡¡¡¡Ê×ÏÈ£¬ÎÒÃÇн¨Ò»¸öMVC¿Õ°×ÏîÄ¿£¬Ìí¼ÓºÃjQuery¡¢jQueryUI¡¢JqgridµÄÇ°¶Ë¿ò¼Ü´úÂ룺
¡¡¡¡È»ºóÔÚViewsµÄHomeÎļþ¼ÐÏÂн¨ÊÓͼ¡°Index.aspx¡±£¬ÔÚÊÓͼµÄbody±êÇ©ÖÐÌí¼ÓÈçÏÂHTML´úÂ룺
<div> <table id="table1"> </table> <div id="div1"> </div> </div> ½Ó×Åн¨Scripts\HomeÎļþ¼Ð£¬ÔÚ¸ÃĿ¼Ð½¨¡°Index.js¡±Îļþ£¬²¢ÔÙÊÓͼÖÐÒýÓ㬴úÂëÈçÏ£ºjQuery(document).ready(function () { //jqGrid³õʼ»¯ jQuery("#table1").jqGrid({ url: '/Home/UserList', datatype: 'json', mtype: 'POST', colNames: ['µÇ¼Ãû', 'ÐÕÃû', 'ÄêÁä', 'ÊÖ»úºÅ', 'ÓÊÏäµØÖ·', '²Ù×÷'], colModel: [ { name: 'UserId', index: 'UserId', width: 180, editable: true }, { name: 'UserName', index: 'UserName', width: 200, editable: true }, { name: 'Age', index: 'Age', width: 150, editable: true }, { name: 'Tel', index: 'Tel', width: 150, editable: true }, { name: 'Email', index: 'Email', width: 150, editable: true }, { name: 'Edit', index: 'Edit', width: 150, editable: false, align: 'center' } ], pager: '#div1', postData: {}, rowNum: 5, rowList: [5, 10, 20], sortable: true, caption: 'Óû§ÐÅÏ¢¹ÜÀí', hidegrid: false, rownumbers: true, viewrecords: true }).navGrid('#div1', { edit: false, add: false, del: false }) .navButtonAdd('#div1', { caption: "±à¼", buttonicon: "ui-icon-add", onClickButton: function () { var id = $("#table1").getGridParam("selrow"); if (id == null) { alert("ÇëÑ¡ÔñÐУ¡"); return; } if (id == "newId") return; $("#table1").editRow(id); $("#table1").find("#" + id + "_UserId").attr("readonly","readOnly"); $("#table1").setCell(id, "Edit", "<input id='Button1' type='button' value='Ìá½»' onclick='Update(\\"" + id + "\\")' /><input id='Button2' type='button' value='È¡Ïû' onclick='Cancel(\\"" + id + "\\")' />"); } }).navButtonAdd('#div1', { caption: "ɾ³ý", buttonicon: "ui-icon-del", onClickButton: function () { var id = $("#table1").getGridParam("selrow"); if (id == null) { alert("ÇëÑ¡ÔñÐУ¡"); return; } Delete(id); } }).navButtonAdd('#div1', { caption: "ÐÂÔö", buttonicon: "ui-icon-add", onClickButton: function () { $("#table1").addRowData("newId", -1); $("#table1").editRow("newId"); $("#table1").setCell("newId", "Edit", "<input id='Button1' type='button' value='Ìá½»' onclick='Add()' /><input id='Button2' type='button' value='È¡Ïû' onclick='Cancel(\\"newId\\")' />"); } }); }); //È¡Ïû±à¼×´Ì¬ function Cancel(id) { if (id == "newId") $("#table1").delRowData("newId"); else $("#table1").restoreRow(id); } //Ïòºǫ́ajaxÇëÇóÐÂÔöÊý¾Ý function Add() { var UserId = $("#table1").find("#newId" + "_UserId").val(); var UserName = $("#table1").find("#newId" + "_UserName").val(); var Age = $("#table1").find("#newId" + "_Age").val(); var Tel = $("#table1").find("#newId" + "_Tel").val(); var Email = $("#table1").find("#newId" + "_Email").val(); $.ajax({ type: "POST", url: "/Home/Add", data: "UserId=" + UserId + "&UserName=" + UserName + "&Age=" + Age + "&Tel=" + Tel + "&Email=" + Email, success: function (msg) { alert("ÐÂÔöÊý¾Ý: " + msg); $("#table1").trigger("reloadGrid"); } }); } //Ïòºǫ́ajaxÇëÇó¸üÐÂÊý¾Ý function Update(id) { var UserId = $("#table1").find("#" + id + "_UserId").val(); var UserName = $("#table1").find("#" + id + "_UserName").val(); var Age = $("#table1").find("#" + id + "_Age").val(); var Tel = $("#table1").find("#" + id + "_Tel").val(); var Email = $("#table1").find("#" + id + "_Email").val(); $.ajax({ type: "POST", url: "/Home/Update", data: "UserId=" + UserId + "&UserName=" + UserName + "&Age=" + Age + "&Tel=" + Tel + "&Email=" + Email, success: function (msg) { alert("ÐÞ¸ÄÊý¾Ý: " + msg); $("#table1").trigger("reloadGrid"); } }); } //Ïòºǫ́ajaxÇëÇóɾ³ýÊý¾Ý function Delete(id) { var UserId = $("#table1").getCell(id, "UserId"); $.ajax({ type: "POST", url: "/Home/Delete", data: "UserId=" + UserId, success: function (msg) { alert("ɾ³ýÊý¾Ý: " + msg); $("#table1").trigger("reloadGrid"); } }); }
¶þ¡¢ÊµÏÖ¿ØÖƲãÒµÎñ
¡¡¡¡ÔÚControllersĿ¼ÏÂн¨¿ØÖÆÆ÷¡°HomeController.cs¡±£¬Index.jsÖвúÉúÁËËĸöajaxÇëÇ󣬶ÔÓ¦¿ØÖƲãÒ²ÓÐËĸöÒµÎñ·½·¨¡£HomeController´úÂëÈçÏ£º
public class HomeController : Controller { UserModel userModel = new UserModel(); public ActionResult Index() { return View(); } /// <summary> /// »ñÈ¡È«²¿Óû§ÁÐ±í£¬Í¨¹ýjson½«Êý¾ÝÌṩ¸øjqGrid /// </summary> public JsonResult UserList(string sord, string sidx, string rows, string page) { var list = userModel.FindAll(); int i = 0; var query = from u in list select new { id = i++, cell = new string[]{ u["UserId"].ToString(), u["UserName"].ToString(), u["Age"].ToString(), u["Tel"].ToString(), u["Email"].ToString(), "-" } }; var data = new { total = query.Count() / Convert.ToInt32(rows) + 1, page = Convert.ToInt32(page), records = query.Count(), rows = query.Skip(Convert.ToInt32(rows) * (Convert.ToInt32(page) - 1)).Take(Convert.ToInt32(rows)) }; return Json(data, JsonRequestBehavior.AllowGet); } /// <summary> /// ÏìÓ¦JsµÄ¡°Add¡±ajaxÇëÇó£¬Ö´ÐÐÌí¼ÓÓû§²Ù×÷ /// </summary> public ContentResult Add(string UserId, string UserName, int Age, string Tel, string Email) { Document doc = new Document(); doc["UserId"] = UserId; doc["UserName"] = UserName; doc["Age"] = Age; doc["Tel"] = Tel; doc["Email"] = Email; try { userModel.Add(doc); return Content("Ìí¼Ó³É¹¦"); } catch { return Content("Ìí¼Óʧ°Ü"); } } /// <summary> /// ÏìÓ¦JsµÄ¡°Delete¡±ajaxÇëÇó£¬Ö´ÐÐɾ³ýÓû§²Ù×÷ /// </summary> public ContentResult Delete(string UserId) { try { userModel.Delete(UserId); return Content("ɾ³ý³É¹¦"); } catch { return Content("ɾ³ýʧ°Ü"); } } /// <summary> /// ÏìÓ¦JsµÄ¡°Update¡±ajaxÇëÇó£¬Ö´ÐиüÐÂÓû§²Ù×÷ /// </summary> public ContentResult Update(string UserId, string UserName, int Age, string Tel, string Email) { Document doc = new Document(); doc["UserId"] = UserId; doc["UserName"] = UserName; doc["Age"] = Age; doc["Tel"] = Tel; doc["Email"] = Email; try { userModel.Update(doc); return Content("Ð޸ijɹ¦"); } catch { return Content("ÐÞ¸Äʧ°Ü"); } } }
Èý¡¢ÊµÏÖÄ£ÐͲãÊý¾Ý·ÃÎÊ
¡¡¡¡×îºó£¬ÎÒÃÇÔÚModelsн¨Ò»¸öHomeÎļþ¼Ð£¬Ìí¼ÓÄ£ÐÍ¡°UserModel.cs¡±£¬ÊµÏÖMongoDBÊý¾Ý¿â·ÃÎÊ´úÂëÈçÏ£º
public class UserModel { //Á´½Ó×Ö·û´®(´Ë´¦Èý¸ö×Ö¶ÎÖµ¸ù¾ÝÐèÒª¿ÉΪ¶ÁÅäÖÃÎļþ) public string connectionString = "mongodb://localhost"; //Êý¾Ý¿âÃû public string databaseName = "myDatabase"; //¼¯ºÏÃû public string collectionName = "userCollection"; private Mongo mongo; private MongoDatabase mongoDatabase; private MongoCollection<Document> mongoCollection; public UserModel() { mongo = new Mongo(connectionString); mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase; mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>; mongo.Connect(); } ~UserModel() { mongo.Disconnect(); } /// <summary> /// Ôö¼ÓÒ»ÌõÓû§¼Ç¼ /// </summary> /// <param name="doc"></param> public void Add(Document doc) { mongoCollection.Insert(doc); } /// <summary> /// ɾ³ýÒ»ÌõÓû§¼Ç¼ /// </summary> public void Delete(string UserId) { mongoCollection.Remove(new Document { { "UserId", UserId } }); } /// <summary> /// ¸üÐÂÒ»ÌõÓû§¼Ç¼ /// </summary> /// <param name="doc"></param> public void Update(Document doc) { mongoCollection.FindAndModify(doc, new Document { { "UserId", doc["UserId"].ToString() } }); } /// <summary> /// ²éÕÒËùÓÐÓû§¼Ç¼ /// </summary> /// <returns></returns> public IEnumerable<Document> FindAll() { return mongoCollection.FindAll().Documents; } }
ËÄ¡¢Ð¡½á
¡¡¡¡´úÂëÏÂÔØ£ºhttp://files.cnblogs.com/lipan/MongoDB_003.rar
¡¡¡¡×Ô´ËΪֹһ¸ö¼òµ¥MongoDB±í¸ñÊý¾Ý²Ù×÷µÄ¹¦ÄܾÍʵÏÖÍê±ÏÁË£¬ÏàÐŶÁÕßÔÚ¿´ÍêÕâƪÎÄÕº󣬲¶à¶¼¿ÉÒÔÇáËÉʵÏÖMongoDBÏîÄ¿µÄ¿ª·¢Ó¦ÓÃÁË¡£´ÏÃ÷µÄÄãÒ»¶¨»á±È±¾ÎÄ×öµÄ¹¦ÄܸüÍêÉÆ£¬¸üºÃ¡£ÏÂƪ¼Æ»®½²½âlinqµÄ·½Ê½·ÃÎÊÊý¾Ý¼¯ºÏ¡£
ÔÎĵØÖ·£ºhttp://www.cnblogs.com/lipan/archive/2011/03/11/1980227.html
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ