Issue
Dear Community, I am facing a problem in redirecting my angular js controller to c# controller.Below is my scrip controller code and c# controller code.Am i making any mistakes in url or is there any routing needed to take my angular controller to c# controller.When the controller is hit,it calls error callback with error alert instantly.please provide me with a solution.
angular controller code:
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); }
}
}
}
}]);
My C# Controller code:
[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);
}
Solution
first this first you need to correct your spelling mistakes in code that is BIG mistake in programming. Try this type of data C# class like this
public class EmployeeInfo
{
[Key]
public int EmpNo { get; set; }
public string EmpName { get; set; }
public decimal Salary { get; set; }
}
js object like this
var Employee = {
EmpNo: $scope.EmpNo,
EmpName: $scope.EmpName,
Salary: $scope.Salary,
};
http part change like this
$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
}
hope this will help you
Answered By - Fazal Qayyum
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.