Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having form with multiple fields and a table.

now i want to pass the form data to contoller to save in database,
i am using ajax to do the same.

What I have tried:

view code

function sumbit() {

        var empFirstName    = document.getElementById('txtFirstName').value;
        var empLastName     = document.getElementById('txtLastName').value;
        var empEmail        = document.getElementById('txtEmail').value;
        var empNumber       = document.getElementById('txtNumber').value;
        var empAddLine1     = document.getElementById('txtAddLine1').value;
        var empAddLine2     = document.getElementById('txtAddLine2').value;


        var game = new Array();
        $('#empTable tr').each(function (row, tr) {
            game[row] = {
                "Game": $(tr).find('td').eq(1).text(),
                "Player Category": $(tr).find('td').eq(2).text(),
                "Player Sub Category": $(tr).find('td').eq(3).text(),
                "Player Achievement": $(tr).find('td').eq(4).text()
            }
        });  
        
        
        var formData = new FormData();
        formData.append("FirstName", empFirstName);
        formData.append("LastName", empLastName);
        formData.append("Email", empEmail);
        formData.append("Number", empNumber);
        formData.append("AddLine1", empAddLine1);
        formData.append("AddLine2", empAddLine2);
        formData.append("Game", JSON.stringify(game));
        
       
        
        $.ajax({

            url: "/Home/Submit",
            type: "POST",
            contentType: false,
            processData: false,
            dataType: "JSON",
           data: formData,         
            success: function (r) {
                alert(r + " record(s) inserted.");
            }
        });
    }


controller

[HttpPost]
        public ActionResult Submit(FormCollection formCode)
        {
            string i,j;
            string FirstName = formCode["FirstName"].ToString();
            string LastName = formCode["LastName"].ToString();
            string Email = formCode["Email"].ToString();
            string Number = formCode["Number"].ToString();
            string AddLine1 = formCode["AddLine1"].ToString();
            string empId = formCode["AddLine2"].ToString();
            string Game= formCode["Game"].ToString();
            
            return View();
            
        }


and i have tried below code also

Submit(string FirstName,string LastName,string Email,string Number,string AddLine1,string AddLine2,string[] Game)


data is coming in all the field, but problem is that value in game is single

[{"Game":"","Player Category":"","Player Sub Category":"","Player Achievement":""},{"Game":"qq","Player Category":"aa","Player Sub Category":"cc","Player Achievement":"zz"},{"Game":"qq","Player Category":"aa","Player Sub Category":"cc","Player Achievement":"zz"}]



I have to save the data of table in database row wise, so it difficult to handle.

i want to pass as array , so i can save easily.
Posted
Updated 18-Jun-19 1:29am

1 solution

You may want to look into using the Array.push() method
Array.prototype.push() - JavaScript | MDN[^]

JavaScript
var game = new Array();
$('#empTable tr').each(function (row, tr) {
 game.push({
    "Game": $(tr).find('td').eq(1).text(),
    "Player Category": $(tr).find('td').eq(2).text(),
    "Player Sub Category": $(tr).find('td').eq(3).text(),
    "Player Achievement": $(tr).find('td').eq(4).text()
  });
}
 
Share this answer
 
Comments
manish-gusain8909 18-Jun-19 8:03am    
game is having value and it passing to controller also but all values are coming in single string. which i want in array form.
MadMyche 18-Jun-19 9:28am    
And the Push() method would be used to add an element to an existing array
manish-gusain8909 19-Jun-19 1:48am    
value is coming to array but in controller its coming like string
MadMyche 19-Jun-19 6:47am    
The value is coming in as a string as you told it to with JSON.stringify()
https://www.w3schools.com/js/js_json_stringify.asp

You will need to add in code on the controller to deserialize the value at the server.

Newtonsoft has JSON.Net to help you with this
https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900