为什么我的 ajax 向控制器发送空值?
Why my ajax sending null values to the controller?
我使用 AJAX 向数据库添加和编辑数据。我已经尝试了所有方法,但返回到我的 [HttpPost] 操作方法的最终值为空。
ajax 文件:
function AddUpdateValue() {
$(document).ready(function () {
var upid = $('#hide').val();
if(upid=="00000000-0000-0000-0000-000000000000"){
$(document).on("click", "#updateEditBtn",
function(){
var formData = {
CompanyEmail: $('#txtCompanyEmail').val(),
IntegrationName: $('#txtIntegrationName').val()
}
console.log(formData.CompanyEmail+" "+formData.IntegrationName+" "+upid+" First for Add");
var forgeryIdAdd = $('#forgeryToken').val();
$.ajax({
url: '/Integrations/AddEditIntegration',
data: {val:formData, rowNum: upid},
headers:{'VerificationToken':forgeryIdAdd},
type: 'POST',
success: function () {
toastr.success("Data successfully added");
},
error: function (jqXHR) {
toastr.warning("Data not added "+jqXHR.statusText +jqXHR.status);
}
});
});
}
else{
var formData = {
CompanyEmail: $('#txtCompanyEmail').val(),
IntegrationName: $('#txtIntegrationName').val()
}
console.log(formData.CompanyEmail+" "+formData.IntegrationName+" "+upid+" Second for edit");
var forgeryIdAdd = $('input[name="__RequestVerificationToken"]').val();
$.ajax({
url: '/Integrations/AddEditIntegration',
data:{val:formData, rowNum:upid},
headers: { 'VerificationToken': forgeryId },
type: 'POST',
dataType: 'json',
success: function (result) {
if (result.isValid) {
toastr.success("Information save successfully.");
$('#Popup').dialog('destroy').remove();
ReloadEmailTemplateGrid();
}
else {
toastr.error("Information saving failed.");
}
$body.removeClass("loading");
},
error: function (jqXHR) {
toastr.error('Request Status: ' + jqXHR.status + ' Status Text: '
+ jqXHR.statusText + ' ' + jqXHR.responseText);
$body.removeClass("loading");
}
});
}
});
}
Post 控制器
[HttpPost]
[AjaxValidateAntiForgeryToken]
public ActionResult AddEditIntegration ([Bind(Exclude ="IntegrationId")]Integrations val, Guid? rowNum)
{
if (rowNum.HasValue)
{
IntegrationsProcessor db = new IntegrationsProcessor(new MCNIDbContext());
Integrations temp = db.GetIntegrationData(rowNum.Value);
temp.IntegrationName = val.IntegrationName;
temp.CompanyEmail = val.CompanyEmail;
db.UpdateIntegrationRow(temp);
return RedirectToAction("Details");
}
else
{
IntegrationsProcessor db = new IntegrationsProcessor(new MCNIDbContext());
Guid newUser = Guid.NewGuid();
Integrations item = new Integrations();
val.IntegrationId = newUser;
db.AddRow(item);
return RedirectToAction("Details");
}
}
防伪令牌,其在 formData 中接收的数据,一切正常,只是它 returns 为空。
为什么不用这个ajax
.....
var formData = {
CompanyEmail: $('#txtCompanyEmail').val(),
IntegrationName: $('#txtIntegrationName').val() ,
rowNum: upid
};
.....
$.ajax({
url: '/Integrations/AddEditIntegration',
data: formData,
.....
并将 rowNum 添加到集成
public class Integrations
{
....
public string CompanyEmail {get; set;}
public string IntegrationName {get; set;}
public Guid? RowNum {get; set;}
.....
}
也调整动作
public ActionResult AddEditIntegration (Integrations integrations)
我使用 AJAX 向数据库添加和编辑数据。我已经尝试了所有方法,但返回到我的 [HttpPost] 操作方法的最终值为空。
ajax 文件:
function AddUpdateValue() {
$(document).ready(function () {
var upid = $('#hide').val();
if(upid=="00000000-0000-0000-0000-000000000000"){
$(document).on("click", "#updateEditBtn",
function(){
var formData = {
CompanyEmail: $('#txtCompanyEmail').val(),
IntegrationName: $('#txtIntegrationName').val()
}
console.log(formData.CompanyEmail+" "+formData.IntegrationName+" "+upid+" First for Add");
var forgeryIdAdd = $('#forgeryToken').val();
$.ajax({
url: '/Integrations/AddEditIntegration',
data: {val:formData, rowNum: upid},
headers:{'VerificationToken':forgeryIdAdd},
type: 'POST',
success: function () {
toastr.success("Data successfully added");
},
error: function (jqXHR) {
toastr.warning("Data not added "+jqXHR.statusText +jqXHR.status);
}
});
});
}
else{
var formData = {
CompanyEmail: $('#txtCompanyEmail').val(),
IntegrationName: $('#txtIntegrationName').val()
}
console.log(formData.CompanyEmail+" "+formData.IntegrationName+" "+upid+" Second for edit");
var forgeryIdAdd = $('input[name="__RequestVerificationToken"]').val();
$.ajax({
url: '/Integrations/AddEditIntegration',
data:{val:formData, rowNum:upid},
headers: { 'VerificationToken': forgeryId },
type: 'POST',
dataType: 'json',
success: function (result) {
if (result.isValid) {
toastr.success("Information save successfully.");
$('#Popup').dialog('destroy').remove();
ReloadEmailTemplateGrid();
}
else {
toastr.error("Information saving failed.");
}
$body.removeClass("loading");
},
error: function (jqXHR) {
toastr.error('Request Status: ' + jqXHR.status + ' Status Text: '
+ jqXHR.statusText + ' ' + jqXHR.responseText);
$body.removeClass("loading");
}
});
}
});
}
Post 控制器
[HttpPost]
[AjaxValidateAntiForgeryToken]
public ActionResult AddEditIntegration ([Bind(Exclude ="IntegrationId")]Integrations val, Guid? rowNum)
{
if (rowNum.HasValue)
{
IntegrationsProcessor db = new IntegrationsProcessor(new MCNIDbContext());
Integrations temp = db.GetIntegrationData(rowNum.Value);
temp.IntegrationName = val.IntegrationName;
temp.CompanyEmail = val.CompanyEmail;
db.UpdateIntegrationRow(temp);
return RedirectToAction("Details");
}
else
{
IntegrationsProcessor db = new IntegrationsProcessor(new MCNIDbContext());
Guid newUser = Guid.NewGuid();
Integrations item = new Integrations();
val.IntegrationId = newUser;
db.AddRow(item);
return RedirectToAction("Details");
}
}
防伪令牌,其在 formData 中接收的数据,一切正常,只是它 returns 为空。
为什么不用这个ajax
.....
var formData = {
CompanyEmail: $('#txtCompanyEmail').val(),
IntegrationName: $('#txtIntegrationName').val() ,
rowNum: upid
};
.....
$.ajax({
url: '/Integrations/AddEditIntegration',
data: formData,
.....
并将 rowNum 添加到集成
public class Integrations
{
....
public string CompanyEmail {get; set;}
public string IntegrationName {get; set;}
public Guid? RowNum {get; set;}
.....
}
也调整动作
public ActionResult AddEditIntegration (Integrations integrations)