0

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>
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

How do you go to FamilyInformation Index.cshtml from Edit.cshtml? ===============================EDIT==================================
Providing some solutions for you.

1. Use javascript to get id from URL.
Employee Controller Create Action

if (ModelState.IsValid)
{
    db.Employees.Add(employee);
    db.SaveChanges();

    //change this
    return new RedirectResult(Url.Action("Edit") + "#" + employee.Id);

 }

then you will get url like this https://localhost:44356/Employee/Edit#2

FamilyInformation Index.cshtml(Javascript Version)

<script>
    $(document).ready(function () {
        var hash = window.location.hash; //#2
        var id = hash.replace('#', ''); //2
    });
</script>

Add this javscript code, then you can get id.
(sorry I am not familiar with Angular, for Angular you can read this article How do I parse URL params after a hash with Angularjs?)

FamilyInformation Index.cshtml(Angular Version)

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

2. Use ViewBag to get id
Employee Controller Edit Action
You may have id parameter in Edit Action, put it into ViewBag.ID

public ActionResult Edit(int id)
{
    @ViewBag.ID = id;

    return View

} 

FamilyInformation Index.cshtml

<script>
    $(document).ready(function () {
        var id = '@ViewBag.ID';
    });
</script>

This code is javascript, I am not sure if Angular work as the same way. You can read this Can't pass ViewBag data to AngularJS

Hope these can help you.

Eva Lai
  • 98
  • 6
  • yes from create page to edit page in edit page i have partial view for family information. When i create employee it pass employee id to edit page eg https://localhost:44356/Employee/Edit/2. Now this id i want to save in employee family table i just want to know if i am passing employee id to url how can i display id in any input field? – Waqas Rauf Jan 16 '18 at 07:35
  • 1st option might be work, but in familyinformation.cshtml how i can get id in text inout field? – Waqas Rauf Jan 16 '18 at 09:10
  • I just edited 1st option, ignore javascript version, check out angular version and try if that work. – Eva Lai Jan 16 '18 at 10:35
  • so happy for you :) – Eva Lai Jan 17 '18 at 01:34
  • got any tutorial for upload and display images using .net core ? – Waqas Rauf Jan 17 '18 at 02:00
  • for uploading images 1. https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads 2. https://stackoverflow.com/questions/47125439/uploading-image-asp-net-core for displaying images https://stackoverflow.com/questions/40447338/adding-images-to-asp-net-core – Eva Lai Jan 17 '18 at 05:49
  • kindly i need to know how i can upload image in specific folder and save to database, after save how to show in view and from there i want to edit and delete option as well. kindly need urgent help – Waqas Rauf Jan 18 '18 at 07:43