【问题标题】:I am facing a problem in redirecting my angular js controller to c# controller.Below is my scrip controller code and c# controller code我在将我的 Angular js 控制器重定向到 c# 控制器时遇到问题。下面是我的脚本控制器代码和 c# 控制器代码
【发布时间】:2020-02-19 06:53:32
【问题描述】:

尊敬的社区, 我在将我的角度 js 控制器重定向到 c# 控制器时遇到问题。下面是我的脚本控制器代码和 c# 控制器代码。我在 url 中犯了任何错误,或者是否需要任何路由将我的角度控制器带到 c# 控制器。当控制器被击中,它立即调用带有错误警报的错误回调。请给我一个解决方案。

角度控制器代码:

var httpTimeout = 1800000;
var httpTimeoutSearch = 3600000;
angular.module('MyApp', [])
var app = angular.module('myApp', []);
app.controller('LoginController', ['$scope', '$rootScope', '$http', function ($scope, $rootScope, $http) {
    $scope.username = "";
    $scope.password = "";
    $scope.Login = function () {
        if ($scope.username != null && $scope.username != "") {
            if ($scope.password != null && $scope.password != "") {
                try {
                    $http({
                        method: 'POST',
                        url: '/Account/Maxi',
                        data: { Username: $scope.username, Passord: $scope.password },
                        timeout: httpTimeout,
                    }).then(function successCallback(response) {
                        alert("sucess");
                    }, function errorCallback(response) {
                        alert("error");
                    });
                }
                catch (ex)
                { alert(ex); }
            }
        }

    }
}]);

我的 C# 控制器代码:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Maxi(LoginModel data, string returnUrl)
{
    string strReturn = "";
    string ConStr = "";
    string Code = "";
    if (data.UserName != null)
   {
       if (data.Password != null)
       {


           DataSet ds = new DataSet();
           SqlParameter[] parameters =
            {  

             new SqlParameter( "@name", SqlDbType.VarChar, 20) { Value = data.UserName } ,
             new SqlParameter("@Roll_No", SqlDbType.Int) { Value = data.Password  } ,



            };
           ConStr = "Data Source=" + "192.168.1.9" + ";Initial Catalog=" + "MyFistDataBase" + ";User id=" + "sa" + ";Password=" + "123" + ";";
           using (SqlConnection con = new SqlConnection(ConStr))
           {
               using (SqlCommand cmd = new SqlCommand("Maxi", con))
               {
                   cmd.CommandType = CommandType.StoredProcedure;
                   SqlDataAdapter da = new SqlDataAdapter();
                   cmd.Parameters.AddRange(parameters);
                   da.SelectCommand = cmd;
                   da.Fill(ds);
               }
           }

           string errmsg = "";

           if (errmsg != "")
           {
               Code = "0"; strReturn = errmsg;
           }
           else
           {
               if (ds.Tables.Count > 0)
               {
                   if (ds.Tables[0].Rows.Count > 0)
                   {
                       Code = "1";

                       foreach (DataRow dr in ds.Tables[0].Rows)
                       {
                           strReturn += dr[0].ToString();
                       }
                       if (strReturn == "1")
                       {
                           Console.Write("Updated");
                       }

                   }
                   //TripDT = TripDT.ToShortDateString();
               }

           } 
       } 
   }
    return View(data);
}

【问题讨论】:

    标签: javascript c# angularjs asp.net-mvc


    【解决方案1】:

    首先,您需要纠正代码中的拼写错误,这是编程中的大错误。 试试这种类型的数据 像这样的C#类

     public class EmployeeInfo
        {
           [Key]
            public int EmpNo { get; set; }
            public string EmpName { get; set; }
            public decimal Salary { get; set; }
        }
    

    这样的js对象

     var Employee = {
                EmpNo: $scope.EmpNo,
                EmpName: $scope.EmpName,
                Salary: $scope.Salary,
            };
    

    http部分改成这样

     $http({
                method: "post",
                url: "/Account/Maxi",
                data: Employee
            }).then(function successCallback(response) {
                            alert("sucess");
                }, function errorCallback(response) {
                            alert("error");
                });
    
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Maxi(EmployeeInfo emp)
    {
         your code here
    }
    

    希望对你有帮助

    【讨论】:

    • 我已经进行了您所做的必要更改,但角度控制器仍然返回错误回调。尝试 { $http({ 方法:'POST',数据:obj,url:'/Account/Maxi',
    • 我为您编辑了答案,您可以试试这个并根据需要进行更改。
    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多