0

I'm getting a list in my angular.js file (EditDeleteItem.js) which I'm making based on the selected table name.

The function to send my list is as below:-

$scope.SaveTblRecord = function (list) {
        //alert(JSON.stringify($scope.employeeList));

        $scope.FetchTableName();
        //var Data = $.param({ TblData: $scope.MyTblDataList });
        var itemList = [];
        angular.forEach(list, function (value, key) {
            if (list[key].selected) {
                itemList.push(list[key].selected);
            }
        });

        $scope.ItemsList = [];
        $scope.ItemsList = itemList;

        $http({
            method: "Post",
            url: "/Admin/SaveTblData",
            data: $scope.ItemsList,
        }).success(function (data) {
            $scope.GetTblData($scope.TempName);
        }).error(function (err) {
            alert(err.Message);
        })
    };//SaveTblRecord

Now in my Controller I want to fetch that list based on the selected table name but I can't do it :-

public JsonResult SaveTblData(List<LocationTbl> NewTblList)    //Need To Have TableName here instead of LocationTbl so that selected table name list is fetched.
    {

        string MyTableName = Convert.ToString(TempData["TableName"]);

        try
        {
            if (NewTblList == null)
            {
                return new JsonResult { Data = "Empty Selection", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
            else {
                using (EBContext db = new EBContext())
                {
                    bool results = false;

                    Type tableType = typeof(CourseDesc);
                    switch (MyTableName)
                    {
                        //case "CourseTbl":
                        //    for (int i = 0; i < NewTblList.Count; i++)
                        //    {
                        //        var CtObj = NewTblList[i];
                        //        CourseTbl ct = db.Courses.AsNoTracking().FirstOrDefault(x => x.ID == CtObj.ID);
                        //        results = UtilityMethods<CourseTbl, int>.EditEntity(db, CtObj);
                        //    }
                        //    break;
                        //case "CourseDescTbl":
                        //    for (int i = 0; i < NewTblList.Count; i++)
                        //    {
                        //        var CdtObj = NewTblList[i];
                        //        CourseDesc cd = db.CourseDesc.AsNoTracking().FirstOrDefault(x => x.ID == CdtObj.ID);
                        //        results = UtilityMethods<CourseDesc, int>.EditEntity(db, CdtObj);
                        //    }
                        //    break;
                        //case "CourseSubDesc":
                        //    for (int i = 0; i < NewTblList.Count; i++)
                        //    {
                        //        var CsdObj = NewTblList[i];
                        //        CourseSubDesc csd = db.CourseSubDesc.AsNoTracking().FirstOrDefault(x => x.ID == CsdObj.ID);
                        //        results = UtilityMethods<CourseSubDesc, int>.EditEntity(db, CsdObj);
                        //    }
                        //    break;
                        //case "InternTbl":
                        //    for (int i = 0; i < NewTblList.Count; i++)
                        //    {
                        //        var ItObj = NewTblList[i];
                        //        InternShip It = db.Interns.AsNoTracking().FirstOrDefault(x => x.ID == ItObj.ID);
                        //        results = UtilityMethods<InternShip, int>.EditEntity(db, ItObj);
                        //    }
                        //    break;
                        case "LocationTbl":
                            for (int i = 0; i < NewTblList.Count; i++)
                            {
                                var LtObj = NewTblList[i];
                                LocationTbl lt = db.Loc.AsNoTracking().FirstOrDefault(x => x.ID == LtObj.ID);
                                results = UtilityMethods<LocationTbl, int>.EditEntity(db, LtObj);
                            }
                            break;
                    }

                    var resultList = new List<object>();
                    foreach (var item in db.Set(tableType))
                    {
                        resultList.Add(item);
                    }

                    return new JsonResult { Data = resultList, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                }

            }

        }
        catch (Exception ex)
        {

            return new JsonResult { Data = ex.Message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    }

I've been searching the internet for a while and found some related links like Link1

and Link2

But I can't find solution to my problem. Please HELP!!

3
  • There is a way to get Type from its name, so you'll need to pass table name from the request. Commented Sep 12, 2017 at 5:34
  • I know I can get table name. The problem is that how can I fetch the list in my c#? Commented Sep 12, 2017 at 14:35
  • You can pass your data in json, when you get the type, then you can deserialize into the type. Commented Sep 13, 2017 at 3:34

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.