Ajax 调用控制器从视图中发送 html table 个对象表单
Ajax calling controller to send a html table of objects form from view
我正在尝试将复杂对象从视图发送到控制器。该对象由 html table 中的对象组成。
当我用 AJAX 调用控制器时,对象列表始终为空。我不知道出了什么问题,因为我已经检查了互联网上的其他代码,但没有发现任何区别,但必须有一些区别。提前致谢。
型号
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 和 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);
}
});
}
和控制器
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!");
}
我修改了控制器、视图和模型。我将不胜感激任何帮助。谢谢
您可以使用这种替代方式通过 AJAX
将您的请求发送到 Controller
方法:
AJAX 通话:
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);
}
});
}
您的 Controller
方法将如下所示:
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!");
}
我正在尝试将复杂对象从视图发送到控制器。该对象由 html table 中的对象组成。 当我用 AJAX 调用控制器时,对象列表始终为空。我不知道出了什么问题,因为我已经检查了互联网上的其他代码,但没有发现任何区别,但必须有一些区别。提前致谢。
型号
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 和 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);
}
});
}
和控制器
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!");
}
我修改了控制器、视图和模型。我将不胜感激任何帮助。谢谢
您可以使用这种替代方式通过 AJAX
将您的请求发送到 Controller
方法:
AJAX 通话:
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);
}
});
}
您的 Controller
方法将如下所示:
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!");
}