Crud with Databases with MongoDB database, Exercises of Java Programming

Crud with Databases with MongoDB database

Typology: Exercises

2020/2021

Uploaded on 05/29/2021

dogukan-kazan
dogukan-kazan 🇬🇧

1 document

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
<add key ="mongoDBHost" value="mongodb://localhost:27017"/>
<add key="mongoDBName" value="mydemo"/>
AccountController.cs
using PROJEADI.Entities;
using PROJEADI.Models;
namespace WebApplication16.Controllers
{
public class AccountController : Controller
{
private AccountModel accountModel = new AccountModel();
public ActionResult Index()
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Crud with Databases with MongoDB database and more Exercises Java Programming in PDF only on Docsity!

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)

}