Ajax calling controller to send a html table of objects form from view

user13200394

What I am trying is to send a complex object from a view to controller. This object is composed by objects from a html table. When I call to the controller with AJAX call, the list of the object is always null. I don't know what is wrong because I have checked other code on internet and I don't find any difference but something has to have. Thanks in advance.

Model

public class Parameter
{
    public string Name { get; set; }
    public string ControlType { get; set; }
    public string ToolTip { get; set; }
    public List<string> CheckedControl { get; set; }

    public int GroupID { get; set; }
    public int CtrIndex { get; set; }
    public List<string> DdItems { get; set; }
    public List<string> LangList { get; set; }
    public List<string> DdMulti { get; set; }

}

Javascript and ajax

    function ajaxSubmit() {
        var table_parameters = [[],[]];
        table_parameters = getSchemaTableData();
        var projectName = document.getElementById('txtProjectName').value;
        var projectID = document.getElementById('txtProjectID').value;


        jQuery.ajax({
            type: "POST",
            url: "@Url.Action("ControlsCreation", "Projects")",
            dataType: "json",
        contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ Parameters: table_parameters, ProjectName: projectName,   ProjectID: projectID }),

            success: function (data) { alert(data); },

        failure: function (errMsg) {
                            alert(errMsg);
                        }
                    });

    }

and the controller

   public JsonResult ControlsCreation(List<Models.Parameter> Parameters, string ProjectName, string ProjectID)
    {
        if (Session["UserID"] == null) return Json("Home/Index");
        string userID = Session["UserID"] as string;


        var mngService = new DBManager(ConfigurationManager.ConnectionStrings["Bugmania2019"].ConnectionString);
        DBLogic dbAccess = new DBLogic(mngService);

        ViewModels.ParametersTable viewModel = new ViewModels.ParametersTable()
        {
            ParametersCollection = Parameters,
            ProjectName = ProjectName,
            ProjectID = ProjectID
        };

        dbAccess.UpdateControls(viewModel);

        return Json("Success!");
    }

I have modified the controller, view and model. I would appreciate any help. thanks

Rahul Sharma

You can use this alternative way to send your request via AJAX to Controller method:

AJAX call:

function ajaxSubmit() {
    var table_parameters = [[],[]];
    table_parameters = getSchemaTableData();
    var projectName = document.getElementById('txtProjectName').value;
    var projectID = document.getElementById('txtProjectID').value;

    var json = {
       table_parameters : table_parameters,
       projectName : projectName,
       projectID : projectID
    };

    jQuery.ajax({
        type: "POST",
        url: "@Url.Action("ControlsCreation", "Projects")",
        dataType: "json",
        data: {"json": JSON.stringify(json)},
        success: function (data) { alert(data); }
        ,
        failure: function (errMsg) {
            alert(errMsg);
        }
    });

}

And your Controller method will look like:

using System.Web.Script.Serialization;

[HttpPost]
public JsonResult ControlsCreation(string json)
{
  var serializer = new JavaScriptSerializer();
  dynamic jsondata = serializer.Deserialize(json, typeof(object));

  //Get your variables here from AJAX call
  var table_parameters = jsondata["table_parameters"];
  var projectName = jsondata["projectName"];
  var projectID = jsondata["projectID"];

        if (Session["UserID"] == null) return Json("Home/Index");
        string userID = Session["UserID"] as string;

        var mngService = new DBManager(ConfigurationManager.ConnectionStrings["Bugmania2019"].ConnectionString);
        DBLogic dbAccess = new DBLogic(mngService);

        ViewModels.ParametersTable viewModel = new ViewModels.ParametersTable()
        {
            ParametersCollection = Parameters,
            ProjectName = ProjectName,
            ProjectID = ProjectID
        };

        dbAccess.UpdateControls(viewModel);

        return Json("Success!");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to send table data from HTML form submit via ajax call to Spring MVC controller

From Dev

send @Model from view to controller with ajax

From Dev

Calling a view controller from index.html

From Dev

Send User class objects from controller to view in laravel 5.4

From Dev

Spring ajax send list of objects from controller to jsp

From Dev

MVC Ajax: How to send string from view to controller

From Dev

How to send data from view to controller laravel with ajax?

From Dev

how to retrieve the array which is send from controller to the view through ajax?

From Dev

Calling Global Variable from Controller to HTML View AngularJS

From Dev

How to pass array of objects from view to controller with AJAX

From Dev

How to send php variable from view(html) to controller

From Dev

Send post from view to controller

From Dev

Send HTML data from a form into a JSON file (Ajax/Jquery)

From Dev

I am trying to send an uploaded file from the view to the controller using an ajax call but the file is received as null in the controller

From Dev

Calling a partial view from AJAX

From Dev

Ajax cannot send form id to MVC Controller

From Dev

Using Ajax to send form data to Django view

From Javascript

Calling Javascript from a html form

From Dev

How to send id from view to Controller CodeIgniter via form_open_multipart

From Dev

Swift calling a class from a view controller

From Dev

Calling parent View Controller function from a subview

From Dev

Calling View Controller Method From TabBarClass

From Dev

calling function from controller in view laravel

From Dev

Calling Viewbag Data from controller to view

From Dev

Calling a method from a different view controller

From Dev

Calling data values from controller to view in Codeigniter

From Dev

how to send id from view to controller in anchor tag in codeigniter in ajax success function?

From Dev

How to send multiple image from MVC view to controller using Ajax in array format?

From Dev

How to send URL id from Asp.net View to Controller using Ajax?

Related Related

  1. 1

    How to send table data from HTML form submit via ajax call to Spring MVC controller

  2. 2

    send @Model from view to controller with ajax

  3. 3

    Calling a view controller from index.html

  4. 4

    Send User class objects from controller to view in laravel 5.4

  5. 5

    Spring ajax send list of objects from controller to jsp

  6. 6

    MVC Ajax: How to send string from view to controller

  7. 7

    How to send data from view to controller laravel with ajax?

  8. 8

    how to retrieve the array which is send from controller to the view through ajax?

  9. 9

    Calling Global Variable from Controller to HTML View AngularJS

  10. 10

    How to pass array of objects from view to controller with AJAX

  11. 11

    How to send php variable from view(html) to controller

  12. 12

    Send post from view to controller

  13. 13

    Send HTML data from a form into a JSON file (Ajax/Jquery)

  14. 14

    I am trying to send an uploaded file from the view to the controller using an ajax call but the file is received as null in the controller

  15. 15

    Calling a partial view from AJAX

  16. 16

    Ajax cannot send form id to MVC Controller

  17. 17

    Using Ajax to send form data to Django view

  18. 18

    Calling Javascript from a html form

  19. 19

    How to send id from view to Controller CodeIgniter via form_open_multipart

  20. 20

    Swift calling a class from a view controller

  21. 21

    Calling parent View Controller function from a subview

  22. 22

    Calling View Controller Method From TabBarClass

  23. 23

    calling function from controller in view laravel

  24. 24

    Calling Viewbag Data from controller to view

  25. 25

    Calling a method from a different view controller

  26. 26

    Calling data values from controller to view in Codeigniter

  27. 27

    how to send id from view to controller in anchor tag in codeigniter in ajax success function?

  28. 28

    How to send multiple image from MVC view to controller using Ajax in array format?

  29. 29

    How to send URL id from Asp.net View to Controller using Ajax?

HotTag

Archive