Controller:
using FullTraining.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FullTraining.Controllers
{
public class StudentController : Controller
{
Context con = new Context();
public ActionResult Index()
{
return View();
}
[HttpPost]
public bool add(Student students)
{
Address add = new Address();
con.student.Add(students);
con.SaveChanges();
return true;
}
}
}
Model:
Address
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace FullTraining.Models
{
public class Address
{
public int Id { get; set; }
public string Add { get; set; }
public int Student_Id { get; set; }
[ForeignKey("Student_Id")]
public virtual Student stud { get; set;}
}
}
Student
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace FullTraining.Models
{
public class Student
{
public Student()
{
add = new Collection<Address>();
}
[Key]
public int Student_Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> add { get; set; }
}
}
Context:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace FullTraining.Models
{
public class Context : DbContext
{
public DbSet<Student> student { get; set; }
public DbSet<Address> Address { get; set; }
}
}
View:
Index:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AngularJS Binding Aspp</title>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/test/sam.js"></script>
</head>
<body>
<div ng-app="userApp">
<div ng-controller="userController">
<form name="empForm">
<input type="text" ng-model="stud.Name" />
<input type="text" ng-model="stud.add[0].Add" />
<input type="submit" class="btn btn-success pull-right" ng-click="apply()" value="Save Claim" />
</form>
</div>
</div>
</body>
</html
>
Javascript:
sam.js
var userApp = angular.module("userApp", []);
userApp.controller("userController", function ($scope, $http) {
$scope.apply = function () {
var ai = {
Name: 'Raja',
add:
{
Add: 'sd'
}
};
$scope.student = [
];
$scope.addRow = function () {
$scope.stuent.push({ 'name': $scope.name, 'employees': $scope.employees, 'headoffice': $scope.headoffice });
$scope.name = '';
$scope.employees = '';
$scope.headoffice = '';
};
$scope.removeRow = function (name) {
var index = -1;
var comArr = eval($scope.student);
for (var i = 0; i < comArr.length; i++) {
if (comArr[i].name === name) {
index = i;
break;
}
}
if (index === -1) {
alert("Something gone wrong");
}
$scope.student.splice(index, 1);
};
$http({
method: 'POST',
url: '/Student/add',
data: JSON.stringify($scope.stud),
headers: { 'Content-Type': 'application/JSON' }
}).
success(function (data) {
alert("Saved Successfully!!!");
})
.error(function (error) {
$scope.status = 'Unable to create a person: ' + error.message;
});
}
});