When I create new employee it redirects to edit page and pass employee id, where I have different employee information.
For example I have create one employee, it is redirect to edit page with emp id. Here I need to add employee family information, but need to save empid in family information table. I just want to know the id I am passing to edit page, how I can display in family infromation page?
This is how I am passing id to edit page
Employee Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
db.Employees.Add(employee);
db.SaveChanges();
return RedirectToAction("Edit", new { id = employee.Id });
}
return View(employee);
}
in url its showing like this https://localhost:44356/Employee/Edit/2
Now this id - 2, I need to save in familyinformation table
EmployeeFamilyController
[HttpPost]
public JsonResult InsertEmployee([FromBody] EmployeFamily emp)
{
db.EmployeeFamily.Add(emp);
db.SaveChanges();
return Json(emp);
}
FamilyInformation Index.cshtml
@{
ViewBag.Title = "AngularJs Tutorial";
}
<style>
</style>
<h2>AngularJs Tutorial : CRUD operation</h2>
<div ng-app="mvcapp" ng-controller="DemoController">
<form name="myform">
<table class="table">
<tr>
<td>Name :</td>
<td>
<input type="text" ng-model="empModel.name" name="Name" placeholder="Name" class='form-control' />
</td>
</tr>
<tr>
<td>Phone :</td>
<td>
<input type="text" ng-model="empModel.phone" name="Phone" placeholder="phone" class='form-control' />
</td>
</tr>
<tr>
<td>Salary :</td>
<td>
<input type="text" ng-model="empModel.salary" name="Salary" placeholder="Salary" class='form-control' />
</td>
</tr>
<tr>
<td>Department :</td>
<td>
<input type="text" ng-model="empModel.department" name="Department" placeholder="Department" class='form-control' />
</td>
</tr>
<tr>
<td>Email :</td>
<td>
<input type="text" ng-model="empModel.emailId" name="EmailId" class='form-control' placeholder="Email" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="button" value="Save" id="btnsave" ng-disabled="isDisabledsave" ng-click="myform.$valid && saveCustomer()" />
<input type="button" value="Update" id="btnupdate" ng-disabled="isDisabledupdate" ng-click="myform.$valid && updateCustomer()" />
</td>
</tr>
</table>
</form>
<table>
<tr>
<th>S.No</th>
<th>
Name
</th>
<th>
Phone
</th>
<th>
Department
</th>
<th>
Salary
</th>
<th>
Email
</th>
</tr>
{{employees}}
<tr ng-repeat="empModel in employees">
<td>{{empModel.id}}</td>
<td>{{empModel.name }}</td>
<td>{{empModel.phone }}</td>
<td>{{empModel.department}}</td>
<td>{{empModel.salary }}</td>
<td>{{empModel.emailId}}</td>
<td>
<a href="" ng-click="getCustomer(empModel)" title="Delete Record">Edit</a> |
<a href="" ng-click="deleteemp(empModel)" title="Delete Record">
Delete
</a>
</td>
</tr>
</table>
</div>
<style>
input[type=button][disabled=disabled] {
opacity: 0.65;
cursor: not-allowed;
}
table tr th {
padding: 10px 30px;
}
table tr td {
padding: 10px 30px;
}
</style>
<script src="~/lib/angular/angular.js"></script>
@*<script src="~/lib/angular-route/angular-route.js"></script>*@
<script>
var angular = angular.module('mvcapp', []);
angular.controller('DemoController', function ($scope, $http) {
GetAllData();
$scope.isDisabledupdate = true;
//Get All Employee
function GetAllData() {
$http.get('/Demo/GetEmployee').then(function (data) {
$scope.employees = data.data;
});
};
//Insert Employee
$scope.saveCustomer = function () {
$http({
method: 'POST',
url: '/Demo/InsertEmployee',
data: JSON.stringify($scope.empModel),
dataType: 'json'
}).then(function () {
console.log($scope.empModel);
GetAllData();
$scope.empModel = null;
alert("Employee Added Successfully!!!");
});
GetAllData();
};
//Delete Employee
$scope.deleteemp = function (empModel) {
varIsConf = confirm('Want to delete ' + empModel.Name + '. Are you sure?');
if (varIsConf) {
$http.delete('/Demo/DeleteEmployee/' + empModel.id).then(function () {
$scope.errors = [];
GetAllData();
alert(empModel.name + " Deleted Successfully!!!");
});
}
};
//Get Employee by id to edit
$scope.getCustomer = function (empModel) {
$http.get('/Demo/getByid/' + empModel.id)
.then(function (data, status, headers, config) {
//;
$scope.empModel = data.data;
GetAllData();
console.log(data);
$scope.isDisabledsave = true;
$scope.isDisabledupdate = false;
});
};
//Update Employee
$scope.updateCustomer = function () {
$http({
method: 'POST',
url: '/Demo/UpdateEmployee',
data: $scope.empModel,
dataType: 'json'
}).then(function () {
GetAllData();
$scope.isDisabledsave = false;
$scope.isDisabledupdate = true;
$scope.empModel = null;
alert("Employee Updated Successfully!!!");
});
};
});
</script>