加入收藏 | 设为首页 | 会员中心 | 我要投稿 十堰站长网 (https://www.0719zz.com/)- 混合云存储、网络、视频终端、云计算、媒体处理!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net 如何接收JSON作为MVC 5操作方法参数

发布时间:2023-02-17 10:57:01 所属栏目:asp.Net 来源:互联网
导读:我一直在试图整个下午爬行通过网络试图接收一个JSON对象在动作控制器。 什么是正确的或更容易的方式去做? 我试过以下: 1: //Post/ Roles/AddUser [HttpPost] public ActionResult AddUser(String model) { if(model != null) { return Json(Success); }el
  我一直在试图整个下午爬行通过网络试图接收一个JSON对象在动作控制器。
  什么是正确的或更容易的方式去做?
 
  我试过以下:
  1:
 
  //Post/ Roles/AddUser
      [HttpPost]
      public ActionResult AddUser(String model)
      {
          if(model != null)
          {
              return Json("Success");
          }else
          {
              return Json("An Error Has occoured");
          }
  
      }
  这给了我一个null值我的输入。
 
  2:
 
  //Post/ Roles/AddUser
      [HttpPost]
      public ActionResult AddUser(IDictionary<string,object> model)
      {
          if(model != null)
          {
              return Json("Success");
          }else
          {
              return Json("An Error Has occoured");
          }
  
      }
  这给我一个500错误在jquery一边是试图发布到它? (意味着它没有正确地绑定)。
 
  这里是我的jQuery代码:
 
  <script>
  function submitForm() {
  
      var usersRoles = new Array;
      jQuery("#dualSelectRoles2 option").each(function () {
          usersRoles.push(jQuery(this).val());
      });
      console.log(usersRoles);
  
      jQuery.ajax({
          type: "POST",url: "@Url.Action("AddUser")",contentType: "application/json; charset=utf-8",dataType: "json",data: JSON.stringify(usersRoles),success: function (data) { alert(data); },failure: function (errMsg) {
              alert(errMsg);
          }
      });
  }
  所有我想做的是在我的mvc操作中接收我的JSON对象?
 
  解决方法
  不幸的是,字典总是在MVC中的模型绑定的问题。 Read the full story here.因此,我们必须创建我们自己的自定义模型绑定器,以获取Dictionary作为我们的控制器操作的参数。
  为了解决你的要求,这里是工作的解决方案 –
 
  首先按照以下方式创建viewmodel。 PersonModel可以有RoleModel列表。
 
  public class PersonModel
  {
      public List<RoleModel> Roles { get; set; }
      public string Name { get; set; }
  }
  
  public class RoleModel
  {
      public string RoleName { get; set;}
      public string Description { get; set;}
  }
  然后有一个索引操作将服务基本索引视图 –
 
  public ActionResult Index()
      {
          return View();
      }
  索引视图将有以下JQuery AJAX POST操作 –
 
  <script src="~/Scripts/jquery-1.10.2.min.js"></script>
  <script>
      $(function () {
          $('#click1').click(function (e) {
  
              var jsonObject = {
                  "Name" : "Rami","Roles": [{ "RoleName": "Admin","Description" : "Admin Role"},{ "RoleName": "User","Description" : "User Role"}]
              };
  
              $.ajax({
                  url: "@Url.Action("AddUser")",type: "POST",data: JSON.stringify(jsonObject),error: function (response) {
                      alert(response.responseText);
              },success: function (response) {
                      alert(response);
                  }
              });
  
          });
      });
  </script>
  
  <input type="button" value="click1" id="click1" />
  索引动作发布到AddUser操作 –
 
  [HttpPost]
      public ActionResult AddUser(PersonModel model)
      {
          if (model != null)
          {
              return Json("Success");
          }
          else
          {
              return Json("An Error Has occoured");
          }
  
      }
  所以现在当post发生时,你可以获取所有发布的数据在action的action参数。

(编辑:十堰站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读