Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Controller:

C#
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
    {
        // GET: Student

        Context con = new Context();
        public ActionResult Index()
        {
            return View();
        }
       
        [HttpPost]
        
        public bool add(Student students)
        {
            // con.Address.Add(students);
            Address add = new Address();

            con.student.Add(students);

            con.SaveChanges();
            return true;
        }
    }
   
}






Model:

Address
C#
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

C#
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:

C#
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:

HTML
<!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

JavaScript
var userApp = angular.module("userApp", []);
userApp.controller("userController", function ($scope, $http) {
    $scope.apply = function () {

        //alert($scope.claims.ClaimsList.ClaimsListType_Id);
        // alert($scope.clg.address.Add);

        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);
        };

     //   console.log(s);
        
        $http({
           
            method: 'POST',
            url: '/Student/add',
            data: JSON.stringify($scope.stud),

            headers: { 'Content-Type': 'application/JSON' }
        }).
         success(function (data) {

             //Showing success message
             alert("Saved Successfully!!!");
             //Updating persons Model
             //getClaim();
         })
         .error(function (error) {



             //Showing error message
             $scope.status = 'Unable to create a person: ' + error.message;
         });
    }
});
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900