
The are 2 ways we can pass data to and from ASP.NET MVC to Angular:
1. By BootStrapping = Using cshtml
2. By Angular $http Service = In this tutorial, we will use this.
The File Structure
This is how we are going to structure our project.

Creating ASP.NET MVC BACK END
- Create the Contact.cs POCO
Create this file under the Models folder. This will be use to store contact information.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
namespace Contacts.Models { public class Contact { public int Id {get;set;} public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Mobile { get; set; } } } - Install the EntityFramework
Using Manage NuGet Package, install the EntityFramework.
- Create the ContactContext.cs
Create this file under Models folder.
- Create Json Camelcase Resolver to convert JSON to Camelcase format.
The AngularJS uses camelcase so we need to a Json converter to convert Pascal case (from C#) to camelcase.
Create a Helper folder (as shown in the file structure above) and add a controller named JsonController.cs.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersusing Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Net; using System.Web.Mvc; namespace Contacts.Helpers { public class JsonController : Controller { // // GET: /Json/ public new ActionResult Json(object data, JsonRequestBehavior behavior) { var jsonSerializerSetting = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; if (Request.RequestType == WebRequestMethods.Http.Get && behavior == JsonRequestBehavior.DenyGet) { throw new InvalidOperationException("GET is not permitted for this request."); } var jsonResult = new ContentResult { Content = JsonConvert.SerializeObject(data, jsonSerializerSetting), ContentType = "application/json" }; return jsonResult; } } } - Create the ContactController.cs
This is where we will be having data exchange with AngularJS.
Under the Controllers folder. Right click on it and add empty controller. Name it as ContactController.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersusing Contacts.Helpers; using Contacts.Models; using System.Web.Mvc; namespace Contacts.Controllers { public class ContactController : JsonController { private ContactContext ContactsDB = new ContactContext(); public ActionResult GetContacts() { var Contacts = ContactsDB.Contacts; return Json(Contacts, JsonRequestBehavior.AllowGet); } } }
Part 4 : Communicating with ASP.NET MVC
12 comments
i m on part 3 and getting error it says
ReplyError 1 'AngularJSApp.Models.ContactContext' does not contain a definition for 'Contact' and no extension method 'Contact' accepting a first argument of type 'AngularJSApp.Models.ContactContext' could be found (are you missing a using directive or an assembly reference?)
means my ContactContext is Empty, what should i do.
Replyi m new to this world and learning.
if you can teach how to multi national address controller it will be really helpfull
Excelente artigo para nós programadores iniciantes, parabéns.
ReplyGreat Article
ReplyMVC 5 Training
Hey, Nice update. I have a little bit idea about Asp.net MVC when I hosted my website on Myasp.net. But after reading this blog, I get something about CRUD application and How it creates by using Asp.net application. Thanks for sharing and keep blogging new updates like this.
ReplyHi Salman Welji,
ReplyYou have to put below in to ContactContext page and your error has been solved.
public class ContactContext : DbContext
{
public DbSet Contacts { get; set; }
}
Thanks
Its great article.
I'm on this part either and I get this error:
Reply'ContactContext' does not contain a definition for 'Contacts' and no extension method 'Contacts' accepting a first argument of type 'ContactContext' could be found (are you missing a using directive or an assembly reference?)
I just need to go to ContactContext.cs and add
public object Contacts { get; set; } ?
nice blog,
Replyregards
Angular 5 training!
Angular 5 training in hyderabad!
best Angular 5 training institutes!
GOOD POST
Replyhttp://spunksoft.com/course/rpa-training-in-hyderabad/
•Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.We are providing AngularJS training in Velachery.
ReplyFor more details: AngularJs training in velachery
Thanks for sharing this informative blog.
ReplyBudget family rooms in Ooty