Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Json Post不向控制器发送数据_Jquery_Json_Asp.net Mvc 4 - Fatal编程技术网

Jquery Json Post不向控制器发送数据

Jquery Json Post不向控制器发送数据,jquery,json,asp.net-mvc-4,Jquery,Json,Asp.net Mvc 4,我有一个问题,这只是发生在这个观点,我不知道是什么原因。jquery json发布没有向控制器发送任何数据,因为调试后很明显,您可以帮助我吗。 这是密码 $("#btConfirm").click(function () { // alert(3); $.ajax({ url: "@Url.Action("EmployeeDetails", "User")", type: 'Post',

我有一个问题,这只是发生在这个观点,我不知道是什么原因。jquery json发布没有向控制器发送任何数据,因为调试后很明显,您可以帮助我吗。 这是密码

    $("#btConfirm").click(function () {
    //    alert(3);
        $.ajax({
            url: "@Url.Action("EmployeeDetails", "User")",
            type: 'Post',
            data: {
                "empID": $("#empID").val(),alert:("sending"),
                "countryID": $("#dropDown1").val(),
                "genderID": $("#dropDown2").val(),
                "ParentAccountID": $("#dropDown3").val(),
                "accountTypeID": $("#dropDown9").val(),
                "countryCode": $("#countryCode").val(),
                "addressJudiciary": $("#addressJudiciary").val(),
                "addressProvince": $("#addressProvince").val(),
                "addressCity": $("#addressCity").val(),
                "addressStreet": $("#addressStreet").val(),
                "addressBuilding": $("#addressBuilding").val(),
                "addressFloor": $("#addressFloor").val(),
                "addressDescription": $("#addressDescription").val(),
                "addressPhone1": $("#addressPhone1").val(),
                "addressPhone2": $("#addressPhone2").val(),
                "addressEmail": $("#addressEmail").val(),
                "personFName": $("#personFName").val(),
                "personMidName": $("#personMidName").val(),
                "personFamName": $("#personFamName").val(),
                "personMotherName": $("#personMotherName").val(),
                "personFullName": $("#personFullName").val(),
                "personBirthDate": $("#personBirthDate").val(),
                "accountKind": $("#accountKind").val(),
                "accountNumber": $("#accountNumber").val(),
                "accountName": $("#accountName").val(),
                "accountNetAmount": $("#accountNetAmount").val(),
                "empContractNumber": $("#empContractNumber").val(),
                "empIssueDate": $("#empIssueDate").val(),
                "empStartDate": $("#empStartDate").val(),
                "empEndDate": $("#empEndDate").val(),
                "empSalary": $("#empSalary").val(),
                "empHourRate": $("#empHourRate").val(),
                "empDamanNumber": $("#empDamanNumber").val(),
                "empDamanValue": $("#empDamanValue").val(),
                "empTransportation": $("#empTransportation").val()
            },
            dataType: 'json',
            error: function (xhr) {
                alert('Error: ' + xhr.statusText);
            },
            success: function (result) {
                alert(result);
                location.reload();
            }
        });
控制器的代码为

[HttpPost]
    public JsonResult EmployeeDetails(string cl)
    {
        try
        {
            string countryID = Request["countryID"];
            string genderID = Request["genderID"];
            string id = Request["empID"];
            string accountTypeID = Request["accountTypeID"];
            string countryCode = Request["countryCode"];
            string addressJudiciary = Request["addressJudiciary"];
            string addressProvince = Request["addressProvince"];
            string addressCity = Request["addressCity"];
            string addressStreet = Request["addressStreet"];
            string addressBuilding = Request["addressBuilding"];
            string addressFloor = Request["addressFloor"];
            string addressDescription = Request["addressDescription"];
            string addressPhone1 = Request["addressPhone1"];
            string addressPhone2 = Request["addressPhone2"];

            string addressEmail = Request["addressEmail"];
            string personFName = Request["personFName"];
            string personMidName = Request["personMidName"];
            string personFamName = Request["personFamName"];
            string personMotherName = Request["personMotherName"];
            string personBirthDate = Request["personBirthDate"];
            string accountKind = Request["accountKind"];
            string accountNumber = Request["accountNumber"];
            string accountName = Request["accountName"];
            string accountNetAmount = Request["accountNetAmount"];

            string empContractNumber = Request["empContractNumber"];
            string empIssueDate = Request["empIssueDate"];
            string empStartDate = Request["empStartDate"];
            string empEndDate = Request["empEndDate"];
            string empSalary = Request["empSalary"];
            string empHourRate = Request["empHourRate"];
            string empDamanNumber = Request["empDamanNumber"];
            string empDamanValue = Request["empDamanValue"];
            string empTransportation = Request["empTransportation"];

            //inserting country entry
            tblCountry ct = new tblCountry();
            ct.NameLtr = countryID;
            ct.CountryDefault = int.Parse(countryCode);
            isms.tblCountries.Add(ct);
            isms.SaveChanges();

            //inserting address entry
            int cntryID = ct.ID;
            tblAddress add = new tblAddress();
            add.CountryID = cntryID;
            add.Judiciary = addressJudiciary;
            add.Province = addressProvince;
            add.City = addressCity;
            add.Village = null;
            add.Street = addressStreet;
            add.Building = addressBuilding;
            add.Floor = int.Parse(addressFloor);
            add.Description = addressDescription;
            add.Phone1 = addressPhone1;
            add.Phone2 = addressPhone2;
            add.Email = addressEmail;
            isms.tblAddresses.Add(add);
            isms.SaveChanges();

            //inserting person entry
            int addID = add.ID;
            tblPerson prs = new tblPerson();
            prs.Name = personFName;
            prs.MidName = personMidName;
            prs.MotherName = personMotherName;
            prs.FamilyName = personFamName;
            prs.FullName = personFName + " " + personMidName + " " + personFamName;
            prs.BirthDate = DateTime.Parse(personBirthDate);
            if (genderID == "1")
                prs.Salut = "Mr";
            else
                prs.Salut = "Mrs";
            prs.GenderID = int.Parse(genderID);
            prs.BirthPlaceID = addID;
            prs.AddressID = addID;
            prs.HealthStatusID = 1;
            prs.SocialStatusID = 1;
            prs.isDeleted = false;
            isms.tblPersons.Add(prs);
            isms.SaveChanges();

            //inserting Parent Entry
            tblParent prt = new tblParent();
            prt.FatherID = null;
            prt.MotherID = null;
            prt.ResponsibleID = 0;
            prt.ResponsibleType = 0;
            isms.tblParents.Add(prt);
            isms.SaveChanges();

            //inserting account entry
            int prtID = prt.ID;
            tblAccount acct = new tblAccount();
            acct.ParentAccountID = prtID;
            acct.kind = int.Parse(accountKind);
            acct.Number = accountNumber;
            acct.AccountType = int.Parse(accountTypeID);
            acct.NetAmount = double.Parse(accountNetAmount);
            acct.Name = accountName;
            isms.tblAccounts.Add(acct);
            isms.SaveChanges();

            //inserting employee entry
            int actID = acct.ID;
            int prsID = prs.ID;
            tblEmpoyee emp = new tblEmpoyee();
            emp.AccountID = actID;
            emp.PersonID = prsID;
            isms.tblEmpoyees.Add(emp);
            isms.SaveChanges();

            //inserting employeeContract entry
            int empID = emp.ID;
            tblEmpoyeeContract empC = new tblEmpoyeeContract();
            empC.EmployeeID = empID;
            empC.ContractNumber = empContractNumber;
            empC.IssueDate = DateTime.Parse(empIssueDate);
            empC.StartDate = DateTime.Parse(empStartDate);
            empC.EndDate = DateTime.Parse(empEndDate);
            empC.Salary = double.Parse(empSalary);
            empC.HourRate = double.Parse(empHourRate);
            empC.DhamanNumber = empDamanNumber;
            empC.DhamanValue = double.Parse(empDamanValue);
            empC.Transportation = double.Parse(empTransportation);
            isms.tblEmpoyeeContracts.Add(empC);
            isms.SaveChanges();

            return Json("Added");
        }
        catch
        {
            return Json("Error");
        }
    }

我给你举个很简单的例子来回答你的问题

PS:你的问题是你的类没有足够的参数来捕获 来自Ajax的所有数据

前端代码

<script>
    //$("#btConfirm").click(function () {
        $.ajax({
            url: "@Url.Action("aaa" )",
            type: 'Post',
            data: {
                a: "1111",
                b: "1111"
                },
            dataType: 'json',
            error: function (xhr) {
                alert('Error: ' + xhr.statusText);
            },
            success: function (result) {
                alert(result);
                location.reload();
            }
        });
    //})
    </script>
public JsonResult aaa(string a, string b)
        {
            return Json(new {  });
        }
PS:我将使用类发送数据,如果我是你喜欢如下


认真地使用
数据:$('form').serialize()
(并重命名下拉列表以匹配属性名称),将POST方法中的参数设置为模型,这样所有内容都可以正确绑定,并且可以删除所有糟糕的
字符串countryID=Request[…]代码在您的
数据中有
警报:(“发送”),
是否正确?你的控制台中有javascript错误吗?我只是想看看它是否有效,但没有,所以我删除了它,结果还是一样的
namespace WebApplication5.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";

            return View();
        }

        public JsonResult aaa(string a, string b)
        {
            return Json(new {  });
        }

        public JsonResult aaa(SampleClass sc)
        {
            return Json(new { });
        }
    }


    public class SampleClass
    {
        public string a { get; set; }
        public string b { get; set; }
    }
}