当我创建新员工时,它会重定向到编辑页面并传递员工ID,我有不同的员工信息。
例如,我创建了一个员工,重定向到具有emp id的编辑页面。这里我需要添加员工家庭信息,但需要在家庭信息表中保存empid。我只是想知道我正在传递给编辑页面的ID,我如何在家庭信息页面中显示?
这就是我将id传递给编辑页面的方式
员工控制器
[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);
}
在url中显示如下 https://localhost:44356/Employee/Edit/2
现在这个id - 2,我需要保存在家庭信息表
中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>
答案 0 :(得分:0)
如何从Edit.cshtml转到FamilyInformation Index.cshtml?
===============================编辑================== ================
为您提供一些解决方案。
的 1。使用javascript从网址获取ID。
员工控制员创建行动
public class SaleDto
{
public decimal Total { get; set; }
public decimal Discount { get; set; }
public decimal TotalSale
{
get { return Total - Discount; }
}
}
然后你会得到这样的网址https://localhost:44356/Employee/Edit#2
FamilyInformation Index.cshtml(Javascript Version)
if (ModelState.IsValid)
{
db.Employees.Add(employee);
db.SaveChanges();
//change this
return new RedirectResult(Url.Action("Edit") + "#" + employee.Id);
}
添加此javscript代码,然后您就可以获得id
(对不起,我不熟悉Angular,对于Angular,你可以阅读这篇文章How do I parse URL params after a hash with Angularjs?)
FamilyInformation Index.cshtml( Angular Version )
<script>
$(document).ready(function () {
var hash = window.location.hash; //#2
var id = hash.replace('#', ''); //2
});
</script>
<强> 2。使用ViewBag获取ID
员工控制器编辑操作
您可以在编辑操作中使用id参数,将其放入ViewBag.ID中
<div ng-app="myApp" ng-controller="myCtrl">
<input ng-value="myVar">
</div>
<script>
var app = angular.module('myApp', []);
app.controller("myCtrl",["$scope","$location",function($scope,$location){
var hashObject = $location.hash();
$scope.myVar = hashObject;
alert(hashObject);
}]);
</script>
FamilyInformation Index.cshtml
public ActionResult Edit(int id)
{
@ViewBag.ID = id;
return View
}
此代码是javascript,我不确定Angular是否以相同的方式工作。您可以阅读此Can't pass ViewBag data to AngularJS
希望这些可以帮助你。