如何将 ng-bind 值传递给 asp.net 核心视图中的变量?
how passing ng-bind values in to a variable at asp.net core view?
我有一个这样的标签:
<a data-ng-click="showform(@item.CategoryId)">
我在剃须刀中有一个变量“catid”...
@{int catid; }
我想在 'catid'
中保存值
这是我的 js 代码:
app.controller('Result', function ($scope) {
$scope.CatId = 0;
$scope.showform = function (id) {
$scope.CatId = id;
}
});
也许你可以用这种方式。
<input type="hidden" name="catid" id="catid" value="{{Catid}}" />
当您提交表单时,此值传递给 asp.net 核心。
i want to use CatId for a linq code and show category name at view
Can you give me a sample code?
下面是一个工作示例,您可以参考。
<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>
使用 AngularJS $http 服务发出请求
<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>
我的GetCategoryNameById
动作方法用于测试
[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}");
}
测试结果
我有一个这样的标签:
<a data-ng-click="showform(@item.CategoryId)">
我在剃须刀中有一个变量“catid”...
@{int catid; }
我想在 'catid'
中保存值这是我的 js 代码:
app.controller('Result', function ($scope) { $scope.CatId = 0; $scope.showform = function (id) { $scope.CatId = id; } });
也许你可以用这种方式。
<input type="hidden" name="catid" id="catid" value="{{Catid}}" />
当您提交表单时,此值传递给 asp.net 核心。
i want to use CatId for a linq code and show category name at view
Can you give me a sample code?
下面是一个工作示例,您可以参考。
<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>
使用 AngularJS $http 服务发出请求
<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>
我的GetCategoryNameById
动作方法用于测试
[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}");
}
测试结果