ASP.NET MVC - Passing Model to Controller
ASP.NET MVC - Passing Model to Controller
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
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
"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:
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"
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 ifWebTest
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
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; }
}
The ajax section of the View:
The controller method signature: