












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Crud with Databases with MongoDB database
Typology: Exercises
1 / 20
This page cannot be seen from the preview
Don't miss anything!













1-mydemo database account collection "username" : "acc1", "password" : "123", "fullName" : "Account 1", "status" : true c# asp.net web application şablon: MVC Route.config -- > controller->Account Manage Nuget ->MongoDB.Driver Web.config
AccountController.cs using PROJEADI.Entities; using PROJEADI.Models; namespace WebApplication16.Controllers { public class AccountController : Controller { private AccountModel accountModel = new AccountModel(); public ActionResult Index()
ViewBag.accounts = accountModel.findAll(); return View(); } [HttpGet] public ActionResult Add() { return View("Add",new Account()); } [HttpPost] public ActionResult Add(Account account) { accountModel.create(account); return RedirectToAction("Index"); }
if(account.password != null) { currentAccount.password = account.password; } currentAccount.Username = account.Username; currentAccount.FullName = account.FullName; currentAccount.Status = account.Status; accountModel.update(currentAccount); return RedirectToAction("Index"); } } } Account.cs using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; namespace WebApplication16.Entities { public class Account { [BsonId]
public ObjectId Id { get; set; } [BsonElement("username")] public string Username { get; set; } [BsonElement("password")] public string password { get; set; } [BsonElement("fullName")] public string FullName { get; set;
private MongoClient mongoClient; private IMongoCollection accountCollection; public AccountModel() { mongoClient = new MongoClient(ConfigurationManager.AppSettings["mongoDBHost"]); var db = mongoClient.GetDatabase(ConfigurationManager.AppSettings["mongoDBNam e"]); accountCollection = db.GetCollection("account"); } public List findAll() { return accountCollection.AsQueryable().ToList(); } public Account find(string id) { var accountId = new ObjectId(id); return accountCollection.AsQueryable().SingleOrDefault(a=>a.Id==accountI d);
public void create(Account account) { accountCollection.InsertOne(account); } public void update(Account account) { accountCollection.UpdateOne(Builders.Filter.Eq("_id", account.Id), Builders.Update .Set("username", account.Username) .Set("password", account.password) .Set("fullName", account.FullName) .Set("status", account.Status) ); } public void delete(string id)
Username FullName Status Option
@foreach (var account in ViewBag.accounts) {
@account.Username @account.FullName @account.Status
Edit | Delete
}
Add.cshtml @{ Layout = null; }
Password
@Html.PasswordFor(a => a.password)
Full Name
@Html.TextBoxFor(a => a.FullName)
Status
@Html.CheckBoxFor(a => a.Status)
Edit.cshmtl @{ Layout = null; } @model WebApplication16.Entities.Account
Add
@using (Html.BeginForm("Edit", "Account", FormMethod.Post)) {
Username
@Html.TextBoxFor(a => a.Username)
Password
@Html.PasswordFor(a => a.password)
@Html.HiddenFor(a =>a.Id)
}