【问题标题】:Receive a complex object from jquery in mvc controller在 mvc 控制器中从 jquery 接收复杂对象
【发布时间】:2014-03-02 23:28:11
【问题描述】:

我正在尝试将表单中的对象提交到我的 mvc 控制器。 这是js:

<script>
    function submitForm() {
    var usersRoles = new Array;
    jQuery("#dualSelectRoles2 option").each(function () {
        usersRoles.push(jQuery(this).val());
    });
    var model = new Object();
    model.user = jQuery('#selectUser').val();
    model.roleslist = usersRoles;
    console.log('model: ' + 'user: ' + model.user + 'roles: ' + model.roleslist);
    console.log('JSON: ' + JSON.stringify(model));
    jQuery.ajax({
        type: "POST",
        url: "@Url.Action("
        AddUser ")",
        dataType: "json",
        //data: { model: JSON.stringify(usersRoles) },
        data: {
            model: JSON.stringify(model)
        },
        success: function (data) {
            alert(data);
        },
        failure: function (errMsg) {
            alert(errMsg);
        }
    });
}

现在获取所有必要的信息并构建对象“模型”,然后将其发布到控制器。

这是我的视图模型:

//for receiving from form
public class submitUserRolesViewModel
{
    public string userId { get; set; }

    public List<String> rolesList { get; set; }
}

这是我的控制器:

 //Post/ Roles/AddUser
    [HttpPost]       
    public ActionResult AddUser(StrsubmitUserRolesViewModel model)
    {
        if (model != null)
        {          

            return Json("Success");
        }
        else
        {
            return Json("An Error Has occoured");
        }
    }

如何在控制器中接收 json 对象?现在,当 jquery 执行帖子时,我的模型为空。这意味着它没有正确绑定。我敢肯定这里只是出了点小问题。

请指出我哪里出错了。

【问题讨论】:

    标签: javascript jquery asp.net asp.net-mvc asp.net-mvc-5


    【解决方案1】:
    jQuery.ajax({
            type: "POST",
            url: "@Url.Action("AddUser")",
            contentType: "application/json",
            data: JSON.stringify(model),
            success: function (data) { alert(data); },
            error: function (errMsg) {
                alert(errMsg);
            }
        });
    

    【讨论】:

      【解决方案2】:

      我稍微修改了你的 JQuery,它现在按预期工作 -

      <script src="~/Scripts/jquery-1.10.2.min.js"></script>
      <script>
          function submitForm() {
              var roles = ["role1", "role2", "role3"];
              var rolesArray = jQuery.makeArray(roles);
      
              var model = new Object();
              model.userId = 1;
              model.rolesList = rolesArray;
      
              jQuery.ajax({
                  type: "POST",
                  url: "@Url.Action("AddUser")",
                  dataType: "json",
                  contentType: "application/json; charset=utf-8",
                  data: JSON.stringify(model),
                  success: function (data) { alert(data); },
                  failure: function (errMsg) {
                      alert(errMsg);
                  }
              });
          }
      </script>
      
      <input type="button" value="Click" onclick="submitForm()"/>
      

      输出 -

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-23
        • 2014-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        相关资源
        最近更新 更多