how passing ng-bind values in to a variable at asp.net core view?

I have a tag like this : <a data-ng-click="showform(@item.CategoryId)">

I have a variable “catid” in razor… @{int catid; }

I want save value in ‘catid’

this my js code :

       
        app.controller('Result', function ($scope) {           
            $scope.CatId = 0;
            $scope.showform = function (id) {
                $scope.CatId = id;    
            }
        });

Answer

i want to use CatId for a linq code and show category name at view

Can you give me a sample code?

Following is a working example, you can refer to it.

<div ng-app="myApp" ng-controller="Result">
    @foreach (var item in Model)
    {
    <a data-ng-click="showform(@item.CategoryId)">CategoryId is @item.CategoryId</a>
        <br />
    }
    <h2>Name of selected Category is {{CatName}}</h2>
</div>

Make request(s) using AngularJS $http service

<script>
    var app = angular.module('myApp', []);
    app.controller('Result', function ($scope, $http) {
        $scope.CatId = 0;
        $scope.CatName = "";

        $scope.showform = function (id) {
            $scope.CatId = id;

            $http.get("/GetCategoryNameById?catid=" + id)
                .then(function (response) {
                    $scope.CatName = response.data;
                    //your code logic here based on your actual requirement
                });
        }
    });
</script>

My GetCategoryNameById action method for testing purpose

[HttpGet("GetCategoryNameById")]
public IActionResult GetCategoryNameById(int catid)
{
    //your code logic here

    //query data based on received catid

    var category = _dbcontext.Categories.Where(c => c.CategoryId == catid).FirstOrDefault();

    return Ok($"{category.CategoryName}");
}

Test Result

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *