简体   繁体   中英

ASP.NET MVC ajax post function sending null values

My apologies if there is an answer already out there for this, I could not find one.

I have an ajax function that posting data to a controller, but some of the data being sent have null values. Here is my setup:

I have 3 parameters being sent to the controller: data, rptnew and recid. The data parameter is a string containing db column names and data. rptnew and recid only contain one value.

var data = "{'DATEWORKING':'" + $('#HEADER-DATEWORKING').val() +
        "','TRAYS':'" + $('#TRAYS').val() +
        "','M26850':'" + $('#INCOMINGMAIL5').val() +
        "','M26860':'" + $('#INCOMINGMAIL6').val() +
        "','X26930':'" + $('#INCOMINGMAIL9').val() +
        "','T26920':'" + $('#INCOMINGMAIL8').val() +
        "','C2501':'" + $('#INCOMINGMAIL1').val()+ "'}";
rptnew = '0';
recid = '2347';

function post:

function postdata(data, section, recid, rptnew) {
$.ajax
    ({
        type: 'POST',
        url: 'UpdateDB_IR’,
        async: false,
        data: ( data, rptnew, recid ),
        dataType: "json",
    success: function (result) {
        if (result) {
            alert("Data Saved Successfully");
            afterpostfunctions();
        }
        else
            alert(result);
    },
    error: function (result) {
        alert("Error Occured, Try Again");
        console.log(result);
    }
});

Controller:

public ActionResult UpdateDB_IR(DATABASE_RECORDS dbData, string rptNew, string recId)

The problem: dbData contains the columns and values from the variable data. rptNew does not contain the value from rptnew. recId does not contain the value from recid.

If I stringify the data like:

JSON.stringify({ dbData: data, rptNew: rptnew, recId: recid });

dbData does not contain the values from data. rptNew contains the value from rptnew. recId contains the value from recid.

I'm at a loss on how to send all values over correctly to the controller. Any suggestions?

database class:

namespace REPORTSYS.Models
{
using System;
using System.Collections.Generic;

public partial class DATABASE_RECORDS
{
    public int CBAD_ID { get; set; }
    public int TRAYS { get; set; }
    public int M26850 { get; set; }
    public int M26860 { get; set; }
    public int X26930 { get; set; }
    public int T26920 { get; set; }
    public int C2501 { get; set; }
}
}
function postdata(data, section, recid, rptnew) {
$.ajax
    ({
        type: 'POST',
        url: 'UpdateDB_IR’,
        async: false,
        data: { dbData : data, rptNew : rptnew, recId : recid },
        dataType: "json",
        success: function (result) {
        if (result) {
            alert("Data Saved Successfully");
            afterpostfunctions();
        }
        else
            alert(result);
    },
    error: function (result) {
        alert("Error Occured, Try Again");
        console.log(result);
    }
});

Send data with key to the controller. As modified in above function.

Try this on data

data: { dbData: data, rptNew: rptnew, recId: recid }

Don't use stringify.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM