我有这个问题只发生在这个视图中,我不知道是什么原因。 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");
}
}
答案 0 :(得分:0)
我给你一个非常简单的例子来做你的问题,
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:我将使用类来发送数据,如果我喜欢如下
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; }
}
}