gpt4 book ai didi

javascript - 我在将 Angular JS Controller 重定向到 C# Controller 时遇到问题。下面是我的 Scrip Controller 代码和 C# Controller 代码

转载 作者:行者123 更新时间:2023-12-02 21:44:24 25 4
gpt4 key购买 nike

亲爱的社区, 我在将 Angular JS Controller 重定向到 C# Controller 时遇到问题。下面是我的 Scrip Controller 代码和 C# Controller 代码。我在 url 中犯了任何错误吗?或者是否需要任何路由将我的 Angular Controller 带到 C# Controller 。当 Controller 被击中,它立即调用错误回调并发出错误警报。请为我提供解决方案。

Angular Controller 代码:

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# Controller 代码:

[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);
}

最佳答案

首先,您需要纠正代码中的拼写错误,这是编程中的一个大错误。尝试这种类型的数据像这样的 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
}

希望这对你有帮助

关于javascript - 我在将 Angular JS Controller 重定向到 C# Controller 时遇到问题。下面是我的 Scrip Controller 代码和 C# Controller 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60294586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com