Introduction
Objective of this article
- To understand coding standards
- How to create project with folder structure.
Everyone must have knowledge of MVC
Nowadays MVC has more value in every field. Every company looking for candidates who have knowledge of MVC. So this is mandatory to learn MVC pattern & do some coding practice so everyone can get a stand in row of crowd i.e. in IT Industry.
Introduction
As per your project requirements project architecture gets changed. Here I am going to show the project folder structure of MVC with some coding standards which are daily used in life of developers.
Note: This is not mandatory to use this pattern for your project also, as this is up to you.
Step 1:
Create one empty project of MVC template using Visual Studio 2013. Create one table in database with primary key using not null. Then right click on our project & add C# class library for Context i.e. in this library we are going to add .edmx. For this right click on this class class library Add, New Item and then select Visual C# template. From Data section select ADO.NET Entity Data Model & select our table with proper database connect.
Now in our another project create the following directory to achieve architecture of project as in the following image:
DataModel.edmx:
Here you will see DataModel.edmx contain DbContext which is instance represents a combination of the Unit Of Work & Repository patterns such that it can be used to query from database & group together changed that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.
Context:
Common:
If you are going to use some code for common purpose then add this in common folder like Authentication, Enum, Constant, etc.
Content:
Here you can add your css, fonts, images, PDF files, etc.
Controller:
Here you can add your all controllers with each action method in respective controller. For example, suppose for User Controller you need to write action method for add/edit/delete user, etc as follows.
- public ActionResult Index()
- {
- return View("_Index");
- }
- public ActionResult Add()
- {
- return View("_Add");
- }
- public ActionResult Edit()
- {
- return View("_Edit");
- }
- public ActionResult Delete()
- {
- return View("_Delete");
- }
- public ActionResult List()
- {
- return View("_List");
- }
Suppose you want to pass values from your ViewModel to action then you can pass as follows:
- [HttpPost]
- public ActionResult Add(AddViewModel user)
- {
- return View("_Add");
- }
- var userRole;
- var userId;
- ViewBag.userRegisterDate;
The repository pattern is an abstraction. It's purpose is to reduce complexity and make the rest of the code persistent ignorant.Adding a Repository pattern on top of this distances you from the features of your ORM. The main reason for using the repository pattern is so that you can use dependency injection and make your code more testable.
Resources:
Suppose your application will use in all over world then you can add resource files according to each country like French, Spanish, etc.
UnitOfWork:
Performs set of transactions in single set of action. To a multiple repositories and call Save() for all of them at once, which calls SaveChanges() which will turn the unit of work into a transaction.
A) Implementation
B) Interface
ViewModel:
To create ViewModel with respective action result follows naming conventions. Suppose you want to create CRUD operation for which you are going to use ViewModel to declare properties & constructors then use AddViewModel.cs, EditViewModel.cs, DeleteViewModel.cs, IndexViewModel.cs.
For example:
- [Required]
- [Display(Name = "User Id")]
- public int UserId
- {
- get;
- set;
- }
- [Required]
- [Display(Name = "User Name")]
- public string UserName
- {
- get;
- set;
- }
- [Required]
- [Display(Name = "Email Id")]
- [DataType(DataType.EmailAddress)]
- public string Email
- {
- get;
- set;
- }
- [Required]
- [Display(Name = "Phone Number")]
- [DataType(DataType.PhoneNumber)]
- public string Mobile
- {
- get;
- set;
- }
Views:
A) Shared
In Shared folder we declare our _Layout.cshtml page for global use i.e. for use like as a master page.
B) Views
To create view for respective action result follows naming conventions. Suppose you want to create CRUD operation then use _Add.cshtml, _Edit.cshtml, _Delete.cshtml, _Index.cshtml, _List.cshtml.
Many times we use some event, css to achieve good design with our own style sheet then we use class or id. To declare any class first take of naming conventions that this name will be more easy to read & understand the code, for example,
- <div class=”imageProfilePhoto”/>
- <img src=”” id=”imgUserRole”/>
- <button onclick=$(‘#lnkDeleteProfilePhoto’).click()>
- <div>
- @Html.ValidationSummary() @using (Html.BeginForm("Add","Home", FormMethod.Post))
- {
- @Html.EditorForModel()
- <br />
- <input id="submit" type="submit" /> }
- </div>
Summary
This article will help fresher candidates to understand how to create our .NET MVC project from scratch.
Hope you enjoyed this one.

Mukund Narayan JhaPosted Jul 3, 2017, 2:46 AM
How to use viewmodel with generic repository along with unit of work design pattern.
Rupesh KahanePosted Oct 20, 2016, 5:34 AM
Thanks to Subash
SubashPosted Oct 20, 2016, 3:20 AM
Useful info
Rupesh KahanePosted Feb 5, 2016, 2:09 PM
thanks to all Ehsan Sajjad, Mohammed Ibrahim, Humayun Kabir Mamun, Santhakumar Munuswamy, Ankit Saxena, Nanddeep Nachan, supriya tarse, Shubham Kumar, Sibeesh Venu & sreenivasa k also
Ehsan SajjadPosted Feb 5, 2016, 11:57 AM
good article
Mohammed IbrahimPosted Feb 4, 2016, 1:50 PM
nice
Humayun Kabir MamunPosted Feb 3, 2016, 11:05 PM
Nice...
Santhakumar MunuswamyPosted Feb 3, 2016, 3:03 PM
Thanks for nice article
Ankit SaxenaPosted Feb 3, 2016, 11:23 AM
Thanks for sharing.
Nanddeep NachanPosted Feb 3, 2016, 1:18 AM
Nice share
supriya tarsePosted Feb 3, 2016, 12:58 AM
nice
Shubham KumarPosted Feb 3, 2016, 12:54 AM
nice info
Sibeesh VenuPosted Feb 3, 2016, 12:34 AM
Nice Share
sreenivasa kPosted Feb 2, 2016, 2:37 PM
nice
Mohammed IbrahimPosted Feb 2, 2016, 2:10 PM
nice