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.
Things we need:
- MongoDB = https://www.mongodb.org/downloads
- VS 2013/VS 2012
- Download and extract the zip file on folder you can easily access.
- Create a data folder next to the bin folder (same level).
- Open a command window on bin folder (Shift + Right Click on the bin folder).
- Type mongod --dbpath ../data (see Figure 1). Don’t close the command window while creating and running the application.
Figure 1: Type 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.
- Create an ASP.NET MVC project in VS 2012/2013. Name it as mvcMongoDB.
- Change the Authentication to No Aunthentication (no disturbance).
- Install the mongocsharpdriver. On the Package Manager Console, type Install-Package mongocsharpdriver. This will add 2 MongoDB references on the application (see Figure 2).
Figure 2: MongoDB DLL files.
Creating the CRUD Application
The Project Structure will guide you as we go along (See Figure 3).
Figure 3: The Project Structure
Let’s Create the Model, DatabaseContext and Controller:
- Under the Models folder, create the the Country.cs (Document).
- Under the same folder, create the CountryDB.cs (The context).
- 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:
- Compile the application first so that we can apply scaffolding;
- Delete the Index.cshtml under the Views->Home folder. We will recreate this through scaffold;
- Open the HomeController.cs and right click the Index method;
- Click on the add View;
- On the Add View window, select the List on the Template option;
- Select Country (mvcMongoDB.Models) on the Model Class;
- Click the Add button.
Creating the Create View:
- Open the HomeController.cs, right click on the Create method (without [HttpPost] attribute);
- Click the Add View option;
- On the Add View window, select the Create in the Template option;
- Select Country (mvcMongoDB.Models) on the Model Class;
- Click the Add button. The Create.cshtml should be added in the Views->Home folder;
- Open the HomeController.cs, right click on the Edit method (without [HttpPost] attribute);
- Click the Add View option;
- On the Add View window, select the Edit in the Template option;
- Select Country (mvcMongoDB.Models) on the Model Class;
- Click the Add button. The Edit.cshtml should be added in the Views->Home folder;
- Open the HomeController.cs, right click on the Delete method (with [HttpGet] attribute);
- Click the Add View option;
- On the Add View window, select the Delete in the Template option;
- Select Country (mvcMongoDB.Models) on the Model Class;
- Click the Add button. The Delete.cshtml should be added in the Views->Home folder;
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
ReplyHi, 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
ReplyThanks for sharing, I will bookmark and be back again...
MongoDB Training Centers in Chenai
MongoDB Online Training | MongoDB Training in Chennai | MongoDB Training in Chennai
ReplyThanks for sharing useful information. I am giving best MVC online training
Replybest asp.net with MVC online training
asp.net with MVC online training
online asp.net with MVC training
asp.net with MVC
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 |
ReplyPython Training in Bangalore |
Nice update. Thank you for sharing
ReplyExcellent 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.
ReplyDevOps Training in Bangalore | DevOps Training in Bangalore
Thanks for sharing well blog useful to everyone python training in bangalore
ReplyNice blog.
ReplyDevops Training in Bangalore
itEanz
very helpfull blog it was a pleasure reading your blog
Replywould 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
ReplyYour article is perfect thanks for sharing keep updating
data science training in bangalore.html
devops training in bangalore
python training in bangalore
aws training in bangalore
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.
ReplyAWS Training in Bangalore
Python Training in Bangalore
Webtrackker is one only IT company who will provide you best class training with real time working on marketing from last 4 to 8 Years Experience Employee. We make you like a strong technically sound employee with our best class training.
ReplyWEBTRACKKER TECHNOLOGY (P) LTD.
C - 67, sector- 63, Noida, India.
F -1 Sector 3 (Near Sector 16 metro station) Noida, India.
+91 - 8802820025
0120-433-0760
Best SAS Training Institute in delhi
SAS Training in Delhi
SAS Training center in Delhi
Best Sap Training Institute in delhi
Best Sap Training center in delhi
Sap Training in delhi
Best Software Testing Training Institute in delhi
Software Testing Training in delhi
Software Testing Training center in delhi
Best Salesforce Training Institute in delhi
Salesforce Training in delhi
Salesforce Training center in delhi
Best Python Training Institute in delhi
Python Training in delhi
Best Python Training center in delhi
Best Android Training Institute In delhi
Android Training In delhi
best Android Training center In delhi
It's amazing blog, Thanks for sharing
Reply.Net Online Training Hyderabad
Thanks for sharing
Replyaws training in chennai
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
ReplyTo 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.
Replysap abap online courses
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.
ReplyMEAN stack training in bangalore
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
Replynebosh course in chennai
The blog which you have posted is wonderful... Thanks for it...
ReplyCloud Computing Courses in Coimbatore
cloud computing training in coimbatore
Data Science Courses in Bangalore
CCNA Course in Madurai
Digital Marketing Training in Coimbatore
Digital Marketing Course in Coimbatore
Thanks to the admin you have spend a lot for this blog I gained some useful info for you. Keep doing.
ReplySEO Training in Chennai
SEO Course in Chennai
AWS Training in Chennai
Data Science Course in Chennai
Digital Marketing Course in Chennai
SEO Training in Anna Nagar
SEO Training in Adyar
Thanks for your post which gather more knowledge about this topic. I read your blog everything is helpful and effective.
ReplySEO Training in chennai
SEO Course in chennai
Digital marketing course in chennai
Good explanation with appropriate solution.
ReplyGuest posting sites
Education
Thanks for sharing. Keep doing the great work.
ReplyMachine Learning training in Pallikranai Chennai
Pytorch training in Pallikaranai chennai
Data science training in Pallikaranai
Python Training in Pallikaranai chennai
Deep learning with Pytorch training in Pallikaranai chennai
Bigdata training in Pallikaranai chennai
Mongodb training in Pallikaranai chennai
Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…
ReplyUpgrade your career Learn Data Warehousing Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.
android training center in noida sector 62
Replydigital marketing training center in noida sector 15
python training in noida sector 63
sap sd training in noida
devops training in noida
Email marketing training course in noida sector 62
Replydevops training in center noida
blue prism training center in noida
cloud computing training in noida sector 15
hadoop training center in noida
python training in noida sector 15
sap sd training center in noida
Replylinux training center in noida sector 62
Email marketing training course in noida sector 15
salesforce training in noida sector 63
openstack training in noida sector 15
machine learning training in Noida sector 62
AWS training institute center in Noida sector 63
Replyandroid training center in noida sector 15
salesforce training in noida sector 18
sap sd training course in noida
sap fico training in noida
Thanks For Sharing!!! Wonderful blog & good post. It is really very helpful to me, waiting for a more new post. Keep Blogging
ReplyMDSU BCOM TimeTable 2020
RU BCOM TimeTable 2020
RDVV BCOM TimeTable 2020
UOK BCOM TimeTable 2020
Such a very useful Blog. Very interesting to read this article. I have learn some new information.thanks for sharing. data science courses
ReplyThat 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.
Replyangular 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
Your technical information related with java programming is very useful and interesting. Also share updated details about java in your website. Thanks for sharing this article.
Replyhardware and networking training in chennai
hardware and networking training in velachery
xamarin training in chennai
xamarin training in velachery
ios training in chennai
ios training in velachery
iot training in chennai
iot training in velachery
Valuable Information. These questions are very helpful when i am attending interviews.
Replydata science training in chennai
data science training in annanagar
android training in chennai
android training in annanagar
devops training in chennai
devops training in annanagar
artificial intelligence training in chennai
artificial intelligence training in annanagar
Very nice post. Awesome article... Really helpful
Replysap training in chennai
sap training in omr
azure training in chennai
azure training in omr
cyber security course in chennai
cyber security course in omr
ethical hacking course in chennai
ethical hacking course in omr
Superb blog post! And this blog clearly explain about for useful information. I would Thanks for sharing this wonderful content.its very useful to us. Keep it up!
ReplySoftware Testing Training in Chennai
Software Testing Online Training in Chennai
Software Testing Courses in Chennai
Software Testing Training in Bangalore
Software Testing Training in Hyderabad
Software Testing Training in Coimbatore
Software Testing Training
Software Testing Online Training
Keep sharing the good post. Thanks for sharing.
ReplyMachine Learning training in Pallikranai Chennai
Data science training in Pallikaranai
Python Training in Pallikaranai chennai
Bigdata training in Pallikaranai chennai
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.
ReplySelenium Webdriver
Marketing is all about analytics. Talentedge offers Marketing Analytics program to sharpen your skills. To know more visit:-
ReplyReally impressed! Everything is very open and very clear clarification of issues. It contains truly facts. Your website is very valuable. Thanks for sharing.
Replydata scientist training and placement in hyderabad
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.
Replydata scientist course in hyderabad
Thanks for sharing this blog.
ReplyBest budget cottages in Ooty