ASP.NET MVC - Passing Model to Controller

ASP.NET MVC - Passing Model to Controller

arosnerarosner Posts: 35Questions: 8Answers: 1

I am attempting to pass an entire object from the DataTables ajax data variable to an ASP.NET MVC Controller. It appears that I need to stringify the object before passing.

I tried 2 different Javascript scripts:

1) The Javascript in the View looks like:

                "ajax": {
                    "url": "/DatabaseTables/LoadData",
                    "data": {
                        "tableName": "@Model.DatabaseTableRow.Table_With_Schema",
                        "rowCount": @Model.RowCount,

** "databaseTableView": "JSON.stringify(@Model)"
** },
"type": "POST",
"datatype": "json"
},

The model definitions I tried with this Javascript are:
a) public async Task<ActionResult> LoadData(string tableName, int rowCount, DataTableView dataTableView)

Result is the subobjects of dataTableView are all empty.

b) public async Task<ActionResult> LoadData(string tableName, int rowCount, string dataTableView)

Result is dataTableView = "the literal string "JSON.stringify(@Model)".. In other words JSON.stringify did not convert to a JSON string. The tableName and rowCount parameters are fine.

2) The Javascript in the View looks like:

                "ajax": {
                    "url": "/DatabaseTables/LoadData",
                    "data": {
                        "tableName": "@Model.DatabaseTableRow.Table_With_Schema",
                        "rowCount": @Model.RowCount,

** "databaseTableView": JSON.stringify(@Model)
** },
"type": "POST",
"datatype": "json"
},

The model definitions I tried with this Javascript are:
a) public async Task<ActionResult> LoadData(string tableName, int rowCount, DataTableView dataTableView)

Does not hit controller method

b) public async Task<ActionResult> LoadData(string tableName, int rowCount, string dataTableView)

**Does not hit controller method.

Any ideas?

Replies

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    edited December 2023

    Sounds like you might need to use ajax.data as a function to pickup data changes. See the last example in the docs and this running example.

    Kevin

  • arosnerarosner Posts: 35Questions: 8Answers: 1

    "ajax": {
    "url": "/DatabaseTables/LoadData",
    "data": function (d) {
    d.tableName = "@Model.DatabaseTableRow.Table_With_Schema";
    d.rowCount = @Model.RowCount;
    d.databaseTableView = JSON.stringify(@Model);
    },

    Gives me the same error as:

    "ajax": {
    "url": "/DatabaseTables/LoadData",
    "data": {
    "tableName": "@Model.DatabaseTableRow.Table_With_Schema",
    "rowCount": @Model.RowCount,

    ** "databaseTableView": JSON.stringify(@Model)
    ** },
    "type": "POST",
    "datatype": "json"
    },

    When I look in the console tab of the developer tools in Microsoft Edge, I see the following: error

    jQuery.Deferred exception: WebTest is not defined ReferenceError: WebTest is not defined
    at data (https://localhost:7103/DatabaseTables/ViewDatabaseTable/fms.HDCTREASPF:122:6

    When I look at the source that the error is pointing at:

                    "ajax": {
                        "url": "/DatabaseTables/LoadData",
                        "data": function (d) {
                            d.tableName = "fms.HDCTREASPF";
                            d.rowCount = 2;
                            d.databaseTableView = JSON.stringify(WebTest.Models.DatabaseTableView);
                        },    
                        "type": "POST",
                        "datatype": "json"
                    },
    

    Note that when I use a property in a class (i.e. d.tableName, d.rowCount), the values are correct. The value inside the JSON.stringify function is the namespace + class name of the object. I am looking for the contents of "WebTest.Models.DatabaseTableView"

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    WebTest is not defined ReferenceError: WebTest is not defined

    That's not a Datatables error but an error indicating that WebTest is not found within the scope that the Datatable is initialized in. You can put a browser's breakpoint on line 6 (d.databaseTableView = .... ) to see if WebTest is seen within the available scopes.

    Without seeing your code its hard to offer suggestions. Please post a link to your page or a running test case showing the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • arosnerarosner Posts: 35Questions: 8Answers: 1

    I found a workaround for this issue. I was able to convert the sub-objects from the MVC View to JSON using the JSON.NET serialize function. Now the controller method has the objects included in the method parameters of the call.

    The model:

    using Microsoft.AspNetCore.Mvc.Rendering;
    using Newtonsoft.Json;

    namespace WebTest.Models
    {
    public class DatabaseTableView
    {
    public DatabaseTable? DatabaseTableRow { get; set; }
    public List<DatabaseTableColumn>? DatabaseTableColumns { get; set; }
    public int RowCount { get; set; }

        //could not serialize the enire object (this). Causes timeout.
        //serialze individual components instead
    
        public string ToJsonDatabaseTableRow
        { 
            get
            {
                return JsonConvert.SerializeObject(this.DatabaseTableRow);
            }
        }
    
        public string ToJsonDatabaseTableColumns
        {
            get
            {
                return Newtonsoft.Json.JsonConvert.SerializeObject(this.DatabaseTableColumns);
            }
        }
    }
    

    }

    The ajax section of the View:

                    "ajax": {
                        "url": "/DatabaseTables/LoadData",
                        "data": {
                            "rowCount": @Model.RowCount,
                            "databaseTableColumns": "@Model.ToJsonDatabaseTableColumns",
                            "databaseTableRow": "@Model.ToJsonDatabaseTableRow"
                        },
                        "type": "POST",
                        "datatype": "json"
                    },
    

    The controller method signature:

        public async Task<ActionResult> LoadData(int rowCount, string databaseTableColumns, string databaseTableRow)
    
Sign In or Register to comment.