0

I developed a model popup in AngularJs, but I don't have any idea how to submit popup form to an ASP.NET Core MVC action method. The application is developed in Visual Studio using .NET Core 7 and AngularJs version is 1.8.3.

Main page:

<div class="form-group" ng-app="myApp">
    <div style="margin-top:12px;" ng-controller="myCtrl">
        <div ng-repeat="product in products" style="border:1px solid #333;border-radius:5px;padding:16px;margin-left:auto;margin-right:0;margin-bottom:4px; width: 16.6667%;display:inline-block;" align="center">
            @*  Html body here *@
                <input type="button" id="filterButton" ng-click='open(product)' name="add_to_cart" style="margin-top:5px;" class="btn btn-info form-control" value="Add to Cart" />
            
        </div>
    </div>
</div>

<script type="text/javascript">
    angular.module('myApp', ['ui.bootstrap']);
    var app = angular.module('myApp');
    app.controller('myCtrl', function ($scope, $uibModal) {
        $scope.id = 0;
        $scope.product = null;
        $scope.products = @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model));
        $scope.open = function (product) {
            $scope.product = product;
            var modalContent = $uibModal.open({
                templateUrl: '/Products/AddToCart/' + product.EnID,
                controller: 'ModalInstanceCtrl',
                controllerAs: '$ctrl',
                resolve: {
                    product: function () {
                        return $scope.product;
                    }
                }
            });
        }
    });
    app.controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, product) {
        var $ctrl = this;
        $scope.selectedUnitItem = null;
        $scope.inventory = null;
        $scope.productobj = product;
        $ctrl.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
        $ctrl.ok = function () {
            $uibModalInstance.close();
        };
        $scope.onUnitChanged = function (code) {
            $scope.selectedUnitItem = $scope.productobj.Inventories.find(r => r.Code == code);
        }
    });
</script>

AddToCart.csHtml page:

<form asp-action="PostAddToCart" method="post">
    <div class="modal-header btn-info"> 
        <label class="modal-title control-label col-md-11">Add To Cart</label>
        <label class="modal-title control-label col-md-1" ng-click="$ctrl.cancel()">X</label>    
    </div>
    <div class="modal-body" id="modal-body">
            <input type="hidden" id="ProductID" name="ProductID" value="{{productobj.EnID}}" />
            <div class="form-group">
                <label for="Productlist" class="control-label col-md-4">Product Name</label>
                <div class="col-md-8">                
                    <label class="control-label col-md-8">{{productobj.Name}}</label>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">Unit Type</label>
                <div class="col-md-8">
                    <select ng-change="onUnitChanged(selectedUnitItem)" id="UnitCode" name="UnitCode" ng-model="selectedUnitItem" class="form-control col-md-8">
                      <option ng-repeat="inv in productobj.Inventories track by inv.Code" value="{{inv.Code}}">{{inv.UnitChart.UnitChartName}}  -  Price : {{inv.SellingPrice}}</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">Available Stock</label>
                <div class="col-md-8">
                    <label class="control-label col-md-8">{{selectedUnitItem.Quantity}}&nbsp;</label>
                </div> 
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">Order Qty</label>
                <div class="col-md-8">
                   <input ng-pattern="/^(0|[0-9][0-9]*)$/" type="number" id="Quantity" name="Quantity" class="form-control col-md-8" />
                </div>
            </div>
      
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" type="submit" ng-submit="$ctrl.ok()">Add To Cart</button>
        <button class="btn btn-primary" type="button" ng-click="$ctrl.cancel()">Cancel</button>
    </div>
</form>

ASP.NET Core MVC action methods:

[HttpGet] 
public async Task<ActionResult> AddToCart()
{
    return PartialView();
}

[HttpPost] 
public async Task<ActionResult> PostAddToCart(string UnitCode,string ProductID,decimal Quantity)
{
    return PartialView("AddToCart");
}

Can anyone explain how to submit popup data to PostAddToCart?

2
  • 1
    You need to inject the $http service into your ModalInstanceCtr so that you can send the http request . Commented Mar 8, 2024 at 9:34
  • Thanks, The Problem is solved by updating ModalInstanceCtr like below. app.controller('ModalInstanceCtrl', function ($scope, $http, $uibModalInstance, product) { //code here $ctrl.ok = function () { $http({ //Code here }); }; }); Commented Mar 15, 2024 at 9:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.