The process of transferring ViewBag value as a parameter in an AngularJS function

I'm facing an issue where the viewbag value is not being passed as a parameter in ng-init. Can someone guide me on how I can successfully pass the viewbag value as a parameter?

angular.js

    {   $scope.detail = function (Id)

     {

               debugger
               $http.post('/Employees/GetDetail', JSON.stringify({ id: Id }))
               .success(function (result) {
                   debugger
                   $scope.ampdetail = result;
                   $window.location.href = 'Detail/' + Id;
               })
               .error(function (result) {
                   console.log(result);
               });
           }

This is my Controller:

My Controller

       public ActionResult Detail(int empid)
    {
        ViewBag.empid = empid;

        return View();
    }

         [HttpPost]
            public JsonResult GetDetail(int id)
            {
                var employee = (from x in db.employees where x.Id==id
                                     select new {
                                             Id = x.Id,
                                             DeptName = x.TblDepartment.Name,
                                             Name = x.Name,
                                             Salary = x.Salary,
                                             Gender = x.Gender,
                                             City = x.City,
                                             Age = x.Age,
                                             image = x.image
                                     }
                                     );

                return Json(employee, JsonRequestBehavior.AllowGet);
            }


    ----------
    view 
    --------
  <!DOCTYPE html >
<html>
<head>
    <title>Edit Employee</title>
</head>
<body ng-app="app" ng-controller="myController" data-ng-init="detail(@ViewBag.empid)"> // Encountering issues with passing id here??
    <h2>Details</h2>
<div>
    <h4>Employee</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            <label style="display:none">Id</label>
        </dt>
        <dd style="display:none">
            {{ampdetail[0].Id}}
        </dd>
    <dt>
        <label class="control-label col-md-2">DeptName</label>
    </dt>    
        <dd>
            {{ampdetail[0].DeptName}}
        </dd>
        <dt>
            <label class="control-label col-md-2">Name</label>
        </dt>
        <dd>
            {{ampdetail[0].Name}}
        </dd>
        <dt>
            <label class="control-label col-md-2">Gender</label>
        </dt>
        <dd>
            {{ampdetail[0].Gender}}
        </dd>
        <dt>
            <label class="control-label col-md-2">Age</label>
        </dt>
        <dd>
            {{ampdetail[0].Age}}
        </dd>
        <dt>
            <label class="control-label col-md-2">Salary</label>
        </dt>
        <dd>
            {{ampdetail[0].Salary}}
        </dd>
        <dt>
            <label class="control-label col-md-2">City</label>
        </dt>
        <dd>
            {{ampdetail[0].City}}
        </dd>
        <dt>
            <label class="control-label col-md-2">Image</label>
        </dt>
        <dd>
            {{ampdetail[0].image}}
        </dd>
    </dl>
</div>
    @*<script src="~/Scripts/jquery-1.10.2.js"></script>
    <script>
    document.ready(function () {
        alert('@ViewBag.empid')
    });
    </script>*@
</body>

</html>

    }

Answer №1

From my understanding, it seems impossible to achieve because the view is not a razor view.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered when using prisma findUnique with where clause

Trying to set up a Singup API using ExpressJS and Prisma is proving to be a bit challenging. The issue arises when I attempt to verify if a given email already exists in my database. Upon passing the email and password, an error is thrown stating Unknown ...

Troubleshooting issues with ng-controller and ng-model functionality in AngularJS

Just starting out with angular JS and following the instructions. First, I'm using ng-init directive to search data with static data. Before integrating it with Laravel, I want to understand the basics of Angular. Everything was running smoothly unt ...

Can a single-page application be created using Express without utilizing React, Angular, or similar frameworks?

Our codebase is currently exclusively built on express, and we are looking to expand it while transitioning into a single page application. At this point in time, I am hesitant to rework the code using frameworks such as Angular or React to achieve this go ...

The jQuery method .on gathers and retains click events

I created a component that manages a view containing articles with games. In order to prevent memory overload and optimize performance, I implemented a solution where when a user clicks on an article (each having the class "flashgame"), they can choose to ...

Unable to locate module: Issue: Unable to locate 'exports'

Looking to make the switch from RequireJS 2.2 to Webpack 3 for my Angular 1.5 app, I aim to establish a Webpack environment that can accommodate existing RequireJS code while transitioning gradually to Webpack and ES6. The plan is to write tests, refactor ...

jQuery mobile menu: Scroll to content after closing the menu

I am currently working on a project where I am using mmenu for the first time. It's functioning as expected, but there is one particular issue that I would really like to have resolved. Here is the URL: What I am hoping to achieve is that when a men ...

retrieving values from an array in ng-model and displaying them within <input

I am seeking assistance with retrieving values from an array and formatting them into a comma-separated list. Visit this link for reference Within my input tag: <input type="number" ng-model="data.price1[$index]" ng-change="pricecalc1($index)" placeh ...

Disabling cache in AngularJS is being overridden by Chrome's cache

I'm currently encountering an interesting caching issue while using Chrome. My angularjs service looks like this: var url = '/api/acts/' + accountId + '/policy/?filter=NEW_POLICY'; return $http({ ...

"Code snippets customized for React are not functioning properly in the javascriptreact.json file within Visual Studio Code

I'm looking to incorporate some customized React.js code snippets into my Visual Studio Code setup. I am aware of the two files in VSCode that handle this - javascript.json and javascriptreact.json. Although the snippets work well in javascript.json, ...

The Angular directive ng-model is not able to return a value

I'm currently troubleshooting an issue with the filters in an older project. Here's the HTML snippet: <input type="text" class="form-control" ng-model="FilterEventsEdit" ng-change="FilterEvents()" ...

Tips on transferring multiple parameters from controller to resource

within my controller API.logInCheck().get({ nid: $scope.username, pwd: $scope.password, comname: $scope.comtyname }).$promise.then(function(response) { $rootScope.popup('success', JSON.stringify(response)); }, function(err) ...

The toggling feature seems to be malfunctioning as the div section fails to display

I'm facing an issue with my Django project while working on a template. I want to toggle the visibility of a div element between hiding and showing, but the function I used isn't working for some reason. I borrowed the function from a different t ...

Using Angular to display asynchronous data with ngIf and observables

In cases where the data is not ready, I prefer to display a loader without sending multiple requests. To achieve this, I utilize the as operator for request reuse. <div class="loading-overlay" *ngIf="this.indicatorService.loadingIndicators[this?.indic ...

The CSS legend for a FLOT plot chart is being unexpectedly replaced

I am currently exploring the FLOT plot tool and facing difficulty with the legend display. It seems that the CSS styling for the legend is somehow being overridden in my code, resulting in an undesirable appearance of the legend: Even when I try to specif ...

incorporating additional lines into the datatable with the help of jquery

I am attempting to directly add rows to the datatable by assigning values in the .js file through the Metronic theme. However, despite displaying item values during debugging, the values are not being added to the datatable row array and thus not reflect ...

Store the output of JavaScript in a PHP variable

My objective is to convert any image to base 64 and then store the converted string in a PHP variable before inserting it into my database. JavaScript Code: function uploadFile() { if (this.files && this.files[0]) { var FR= new FileReader ...

Transmitting an Array of Objects to a Web API using AngularJS

Throughout this process, I retrieve an array of "vehicles" from the Web API. I then make modifications and perform various actions on each vehicle. Afterwards, I aim to send back the updated list without the need for looping through it again. I have exper ...

JQuery UI Autocomplete: Results, Ctrl + A and Delete

I am currently designing a form that allows users to add members to a project. Only members with existing profiles in the system can be added. The form includes an input field for the member's name and a select box for their position, which is initial ...

How can I filter an array to retain only the initial 5 characters of every element?

I need help using the .map function to transform this array so that only the first 5 characters are displayed, like "01:00", "02:00"? This is the array: ["01:00:00 AM", "02:00:00 AM", "03:00:00 AM", "04:00:00 AM", "05:00:00 AM", "06:00:00 AM", "07:00:00 ...

Troubleshooting problems with data rendering in jQuery

Currently, my goal is to use JQuery to display a menu of checkboxes based on a specific template in a div. To enhance user experience, I have included a search box that will filter the menu items. However, there is an unusual issue occurring. When the men ...