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
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.
Comments