【问题标题】:Ajax calling controller to send a html table of objects form from viewAjax 调用控制器从视图中发送对象表单的 html 表
【发布时间】:2020-04-03 00:53:02
【问题描述】:

我正在尝试将复杂对象从视图发送到控制器。该对象由 html 表中的对象组成。 当我通过 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!");
    }

我已经修改了控制器、视图和模型。我将不胜感激任何帮助。谢谢

【问题讨论】:

    标签: c# html-table asp.net-mvc-5


    【解决方案1】:

    您可以使用这种替代方式将您的请求通过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!");
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2021-09-15
      相关资源
      最近更新 更多