private MongoDatabase GetDB() |
{ |
return MongoDatabase.Create(ConfigurationManager.ConnectionStrings[ "MongoDB" ].ConnectionString); |
|
} |
public ActionResult Create(FormCollection collection) |
{ |
try |
{ |
var db = GetDB(); |
foreach (var key in collection.AllKeys) |
{ |
db[ "testTable" ].Insert( new MongoDB.Bson.BsonDocument{ |
{key, collection[key]} |
}); |
} |
|
return RedirectToAction( "Index" ); |
} |
catch |
{ |
return View(); |
} |
} |
public ActionResult Index() |
{ |
try |
{ |
var db = GetDB(); |
var testTable = db[ "testTable" ].FindAll(); |
var result = new StringBuilder(); |
foreach (var testData in testTable) |
{ |
foreach (var property in testData.Names) |
{ |
result.AppendFormat( "{0}:{1} " , property, testData[property]); |
} |
result.Append( "<br />" ); |
} |
return Content(result.ToString()); |
} |
catch |
{ |
return View(); |
} |
} |
@{ |
ViewBag.Title = "Create" ; |
Layout = "~/Views/Shared/_Layout.cshtml" ; |
} |
<h2>Create</h2> |
<form method= "post" action= "/Home/Create" > |
Name: |
<input name= "name" type= "text" /><br /> |
Age: |
<input name= "age" type= "text" /><br /> |
Gender: |
<input name= "gender" type= "text" /><br /> |
Married: |
<input name= "married" type= "text" /><br /> |
<input type= "submit" value= "Add" /> |
</form> |
by: 发表于:2017-11-02 09:49:35 顶(0) | 踩(0) 回复
??
回复评论