CRUD Application Using ASP.NET MVC 5 and MongoDB

Why use MongoDB? It’s not a fad. It’s a need. I will not cover the advantages of MongoDB over other databases out there. Try this and you will find why.

In this article, I will give you the basics on creating CRUD application using ASP.NET MVC and MongoDB.  Take it from the basics and you can grow it into Enterprise Application.


Download the Complete Source Code


Things we need:
  1. MongoDB = https://www.mongodb.org/downloads
  2. VS 2013/VS 2012


Running the MongoDB/Quick and Easy Way

  1. Download and extract the zip file on folder you can easily access.
  2. Create a data folder next to the bin folder (same level).
  3. Open a command window on bin folder (Shift + Right Click on the bin folder).
  4. Type mongod --dbpath ../data (see Figure 1). Don’t close the command window while creating and running  the application.
Run MongoDB
Figure 1Type mongod –dbpath ../data to run MongoDB

Starting the ASP.NET MVC CRUD Application and Adding Mongocsharpdriver
We will be working on simple Country CRUD with 3 fields.
  1. Create an ASP.NET MVC project in VS 2012/2013. Name it as mvcMongoDB.
  2. Change the Authentication to No Aunthentication (no disturbance).
  3. Install the mongocsharpdriver. On the Package Manager Console, type Install-Package mongocsharpdriver. This will add 2 MongoDB references on the application (see Figure 2).
MongoDBDrivers
Figure 2: MongoDB DLL files.

Creating the CRUD Application

The Project Structure will guide you as we go along (See Figure 3).
Project Structure
Figure 3: The Project Structure

Let’s Create the Model,  DatabaseContext and Controller:
  1. Under the Models folder, create the the Country.cs (Document).
  2. Under the same folder, create the CountryDB.cs (The context).
  3. Open the HomeController.cs (under the Controllers folder), replace with code snippet below:

The Country.CS: Why Document?
In MongoDB, tables are called documents that are similar JSON objects. And the Id is called ObjectId with data type of string. With an attribute of [BsonRepresentation(BsonType.ObjectId)].  This is a GUID  representation in C#.
MongoDB CRUD Methods Used
Method Description
FindAll Returns all records
SetSortOrder Query with Sort Order
Insert Adds New record
FindOneById Finds record by ObjectId
Save Update the record
Remove Remove a record

Creating the Views

Creating the Index/List View:
  1. Compile the application first so that we can apply scaffolding;
  2. Delete the Index.cshtml under the Views->Home folder. We will recreate this through scaffold;
  3. Open the HomeController.cs and right click the Index method;
  4. Click on the add View;
  5. On the Add View window, select the List on the Template option;
  6. Select Country (mvcMongoDB.Models) on the Model Class;
  7. Click the Add button.

Creating the Create View:
  1. Open the HomeController.cs, right click on the Create method (without [HttpPost] attribute);
  2. Click the Add View option;
  3. On the Add View window, select the Create in the Template option;
  4. Select Country (mvcMongoDB.Models) on the Model Class;
  5. Click the Add button. The Create.cshtml should be added in the Views->Home folder;
Creating the Edit View:
  1. Open the HomeController.cs, right click on the Edit method (without [HttpPost] attribute);
  2. Click the Add View option;
  3. On the Add View window, select the Edit in the Template option;
  4. Select Country (mvcMongoDB.Models) on the Model Class;
  5. Click the Add button. The Edit.cshtml should be added in the Views->Home folder;
Creating the Delete View:
  1. Open the HomeController.cs, right click on the Delete method (with [HttpGet] attribute);
  2. Click the Add View option;
  3. On the Add View window, select the Delete in the Template option;
  4. Select Country (mvcMongoDB.Models) on the Model Class;
  5. Click the Add button. The Delete.cshtml should be added in the Views->Home folder;
The Outputs:

The Simple CRUD App.



The CountryDB on RoboMongo.



That’s it. Run the application with the MongoDB running.
If you want to administer you MongoDB database, you can use RoboMongo.

That’s for today. More to come. If you have some questions, please key on the comments section.

48 comments

Thanks alot for the article really helped me alot

Reply

Hi, Nice update. I have a little bit idea about Asp.net when I hosted my website on Myasp.net. But after reading this blog I get something about CRUD application. Thanks for sharing and keep blogging new updates like this.

Reply


Thanks for sharing, I will bookmark and be back again...


MongoDB Training Centers in Chenai

Reply

If wants to get real time Python Training visit this blog They give professional and job oriented training for all students.To make it easier for you Besant Technologies trained as visualizing all the real-world Application and how to implement in Archiecture trained with expert trainners guide may you want.. Start brightening your career with us besant Technologies In Bangalore. Selenium Training in Bangalore |
Python Training in Bangalore |

Reply

Excellent information. You have post an very informative post. I hope you continue to provide new ideas for us all and greetings success always for you. keep sharing such a useful blog for our vision.
DevOps Training in Bangalore | DevOps Training in Bangalore

Reply

Thanks for sharing well blog useful to everyone python training in bangalore

Reply

very helpfull blog it was a pleasure reading your blog
would love to read it more
knowldege is not found but earned through hardwork and good teaching
that being said click here to join us the next best thing in bangalore
devops online training
Devops Training in Bangalore

Reply
This comment has been removed by the author.

It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

AWS Training in Bangalore
Python Training in Bangalore

Reply

I got good information by reading this article.I learned Development and Operations.Thankyou for this articleDevOps Training In Chennai | DevOps Training Institute In Chennai

Reply

To improve Knowledge about the latest and vital technology would increase one's self esteem to the core at the time of lagging confidence.The content presented here is quite resembling the same. You have done a great job by sharing this in here.
sap abap online courses

Reply

I enjoy what you guys are usually up too. This sort of clever work and coverage! Keep up the wonderful works guys I’ve added you guys to my blog roll.
MEAN stack training in bangalore

Reply

Hi there I am so thrilled I found your website, I really found you by mistake, while I was browsing on Yahoo for something else, Anyhow I am here now and would just like to say thanks a lot for a tremendous post and an all-round exciting blog
nebosh course in chennai

Reply
This comment has been removed by the author.

Thanks for your post which gather more knowledge about this topic. I read your blog everything is helpful and effective.
SEO Training in chennai
SEO Course in chennai
Digital marketing course in chennai

Reply

Good explanation with appropriate solution.

Guest posting sites
Education

Reply
This comment has been removed by the author.
This comment has been removed by the author.

Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…

Upgrade your career Learn Data Warehousing Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.

Reply

Thanks For Sharing!!! Wonderful blog & good post. It is really very helpful to me, waiting for a more new post. Keep Blogging
MDSU BCOM TimeTable 2020
RU BCOM TimeTable 2020
RDVV BCOM TimeTable 2020
UOK BCOM TimeTable 2020

Reply
This comment has been removed by the author.

Such a very useful Blog. Very interesting to read this article. I have learn some new information.thanks for sharing. data science courses

Reply

That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
angular js training in chennai

angular js training in tambaram

full stack training in chennai

full stack training in tambaram

php training in chennai

php training in tambaram

photoshop training in chennai

photoshop training in tambaram

Reply

Selenium automation testing has made the process simpler and faster than manual testing. The latest version of selenium is the selenium webdriver which improves functional test coverage. Learn selenium automation testing to drive into the IT field.

Selenium Webdriver

Reply

Marketing is all about analytics. Talentedge offers Marketing Analytics program to sharpen your skills. To know more visit:-

Reply

Really impressed! Everything is very open and very clear clarification of issues. It contains truly facts. Your website is very valuable. Thanks for sharing.
data scientist training and placement in hyderabad

Reply

I think I have never seen such blogs ever before that has complete things with all details which I want. So kindly update this ever for us.
data scientist course in hyderabad

Reply