简体   繁体   English

如何使用 angular js 在 asp.net 核心 mvc 中使用下拉列表保存和编辑数据

[英]How to save and edit data using dropdownlist in asp.net core mvc using angular js

I am using AngularJs in small part of my asp.net core MVC project i am using two dropdownlist and a textbox using textbox i am able to save and edit data but with dropdownlist i am not able to reset dropdownlist after posting the data also i am not able to edit the record with dropdowns.我在我的 asp.net 核心 MVC 项目的一小部分中使用 AngularJs 我正在使用两个下拉列表和一个使用文本框的文本框我能够保存和编辑数据但是使用下拉列表我无法在发布数据后重置下拉列表我也无法使用下拉菜单编辑记录。

Here is my code这是我的代码

<table ng-table="tblCustomers" class="table" cellpadding="0" cellspacing="0">
                <tr>
                    <th>Stakes</th>
                    <th>Supplier</th>
                    <th>Description</th>
                    <th></th>
                </tr>
                <tbody ng-repeat="m in ST">
                    <tr>
                        <td>
                            <span ng-hide="m.EditMode">{{m.stakeName}}</span>
                            <select ng-model="StakeId" value="{{ m.stakeId }}" ng-options="s.stakeName for s in StakeList track by s.stakeId" ng-show="m.EditMode" class="form-control">
                                <option value="">Select Stake</option>
                            </select>
                        </td>
                        <td>
                            <span ng-hide="m.EditMode">{{m.supplierName}}</span>
                            <select ng-model="SupplierId" value="{{ m.supplierId }}" ng-options="a.supplierName for a in SupplierList track by a.supplierId" ng-show="m.EditMode" class="form-control">
                                <option value="">Select Supplier</option>
                            </select>
                        </td>
                        <td>
                            <span ng-hide="m.EditMode">{{m.description}}</span>
                            <input type="text" ng-model="m.description" ng-show="m.EditMode" />
                        </td>
                        <td>
                            <a class="Edit" href="javascript:;" ng-hide="m.EditMode" ng-model="m.stakeTransId" ng-click="Edit($index)">Edit</a>
                            <a class="Update" href="javascript:;" ng-show="m.EditMode" ng-click="Update($index)">Update</a>
                            <a class="Cancel" href="javascript:;" ng-show="m.EditMode" ng-click="Cancel($index)">Cancel</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <td>
                            <select ng-model="StakeId" value="{{ m.stakeId }}" ng-options="s.stakeName for s in StakeList track by s.stakeId" ng-show="m.EditMode" class="form-control">
                                <option value="">Select Share</option>
                            </select>
                    </td>
                    <td>
                            <select ng-model="SupplierId" value="{{ m.supplierId }}" ng-options="a.supplierName for a in SupplierList track by a.supplierId" ng-show="m.EditMode" class="form-control">
                                <option value="">Select Agent</option>
                            </select>
                    </td>

                    <td width="150px">
                        <input ng-model="Description" placeholder="Description" class="form-control" />
                    </td>
                    <td>
                        <input type="button" value="Add" id="Save" ng-click="Savedata()" />
                    </td>
                </tr>
            </table>

Here is my controller code这是我的 controller 代码

[HttpPost]
    public JsonResult AddStakeTrans(int supplierId, int stakeId, string desc)
    {
        StakeTran stakeTrans = new StakeTran();
        shareTrans.SupplierId = supplierId;
        shareTrans.StakeId = stakeId;
        shareTrans.Description = desc;
        db.Insert(stakeTrans);
    
    //after posting data to get inserted record to display in table 
    StakeTransViewModel stakeTransViewModel = new StakeTransViewModel();
    stakeTransViewModel.SupplierName = db.ExecuteScalar<string>("Select SupplierName from Supplier where SupplierId = @0", supplierId);
    stakeTransViewModel.StakeName = db.ExecuteScalar<string>("Select StakeName from Stake where StakeId = @0", stakeId);
    stakeTransViewModel.Description = desc;
    
    //to get the dropdownlist 
    stakeTransViewModel.StakeList = db.Fetch<Stake>("Select StakeId,StakeName from Share").ToList();
    stakeTransViewModel.SupplierList = db.Fetch<Supplier>("Select SupplierId,SupplierId from Supplier").ToList();
    return Json(stakeTransViewModel);
}

Here is my angular code to post and edit data这是我的 angular 代码,用于发布和编辑数据

        $scope.StakeList = "";
        $scope.SupplierList = "";
        $scope.Savedata = function () {
            $http({
                method: "POST",
                url: "/StakeTrans/AddStakeTrans",
                data: $.param({
                    supplierId: $scope.SupplierId.supplierId,
                    stakeId: $scope.StakeId.stakeId,
                    desc: $scope.Description
                }),
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).then(function (data) {
                $scope.ST.StakeName = data.data.StakeName;
                $scope.ST.SupplierName = data.data.SupplierName;
                $scope.StakeList = data.data.stakeList;
                $scope.SupplierList = data.data.supplierList;
                $scope.StakeId = data.data.stakeList;
                $scope.SupplierId = data.data.supplierList;
                $scope.ST.push(data.data);
                $scope.btntext = "Save";
            },function (error) {

            });
            $scope.StakeTransId = "";
            $scope.StakeId = "";
            $scope.StakeName = "";
            $scope.SupplierId = "";
            $scope.SupplierName = "";
            $scope.Description = "";

        };




        //This variable is used to store the original values.
        $scope.EditItem = {};

        //Editing an existing record.
        $scope.Edit = function (index) {

            $scope.ST[index].EditMode = true;

            $scope.EditItem.stakeId = $scope.ST[index].StakeId.stakeId;
            $scope.EditItem.supplierId = $scope.ST[index].SupplierId.supplierId;
            $scope.EditItem.description = $scope.ST[index].description;


//the update code
        };

in edit mode i want to hide the data in the row besides which i have edit button and show textbox and dropdown with value which that particular id have just like we normally have in edit mode and then want to click on update to update the record also in add mode when i click on add button the record get added and shown in the grid successfully but my dropdownlists are not getting reset.在编辑模式下,我想隐藏行中的数据,除此之外我有编辑按钮并显示文本框和下拉列表,其中包含该特定 ID 的值,就像我们通常在编辑模式下一样,然后想要单击更新以更新记录在添加模式下,当我单击添加按钮时,记录会成功添加并显示在网格中,但我的下拉列表没有重置。

So this is the whole problem i am facing and i am not getting the solution for that, can anybody suggest me the solution to how i can successfully get dropdownlist working in add and edit mode using angular js.所以这是我面临的全部问题,我没有得到解决方案,任何人都可以建议我如何使用 angular js 成功让下拉列表在添加和编辑模式下工作的解决方案。

There are two mistakes existed in your controller.您的 controller 存在两个错误。

    [HttpPost]
    public JsonResult AddStakeTrans(int supplierId, int stakeId, string desc)
    {
        StakeTran stakeTrans = new StakeTran();

        //ERROR1
        //shareTrans.SupplierId = supplierId;
        stakeTrans.SupplierId = supplierId;

        //shareTrans.StakeId = stakeId;
        stakeTrans.StakeId = stakeId;

        //shareTrans.Description = desc;
        stakeTrans.Description = desc;

        db.Insert(stakeTrans);
    
    //after posting data to get inserted record to display in table 
    StakeTransViewModel stakeTransViewModel = new StakeTransViewModel();
    stakeTransViewModel.SupplierName = db.ExecuteScalar<string>("Select SupplierName from Supplier where SupplierId = @0", supplierId);
    stakeTransViewModel.StakeName = db.ExecuteScalar<string>("Select StakeName from Stake where StakeId = @0", stakeId);
    stakeTransViewModel.Description = desc;
    
    //to get the dropdownlist 
    stakeTransViewModel.StakeList = db.Fetch<Stake>("Select StakeId,StakeName from Share").ToList();

    //ERROR 2
    //stakeTransViewModel.SupplierList = db.Fetch<Supplier>("Select SupplierId,SupplierId from Supplier").ToList();
    stakeTransViewModel.SupplierList = db.Fetch<Supplier>("Select SupplierId,SupplierName from Supplier").ToList();
     

    return Json(stakeTransViewModel);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM