【问题标题】:Retrieve Json Data as a IEnumerable以 IEnumerable 形式检索 Json 数据
【发布时间】:2021-01-21 03:10:17
【问题描述】:

目标:
将包含许多数据的 json 数据从前端发送到后端。

问题:
当我将数据发送到后端时,我不会将其作为 IEnumerable 检索

我遗漏了代码的哪一部分?

信息:
*使用 JQuery 作为前端
*使用 Asp.net mvc 作为后端

谢谢!

@{
    ViewData["Title"] = "Home Page";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<button class="testtest">
    dfdf
</button>

<script type="text/javascript">

    $('.testtest').click(function () {

        var txt = '{"name":"John", "age":30, "city":"New York"}'
        var obj = JSON.parse(txt);

        $.ajax({
            url: '@Url.Action("TestGet")',
            data:
            {
                contactcollection: obj
            },
            dataType: 'json',
            type: 'Get',
            contentType: 'application/json',
            success: function (result) {

                var display = '';


                return;
            }
        });

    });

</script>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using JsonData.Models;

namespace JsonData.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }


        [HttpGet]
        public JsonResult TestGet(IEnumerable<Contact> contactcollection)
        {

            int ddd = 23;

            return Json(null);
        }
    }

    public class Contact
    {
        public string name;
        public int age;
        public string city;
    }
}

【问题讨论】:

  • 在控制器方法中放置一个断点,然后在 Watch 窗口中检查 Request.Form 的内容。我想看看数据是否存在或根本不存在。

标签: c# json ajax asp.net-mvc jquery-3


【解决方案1】:

无法通过 HttpGet 发送 IEmunerable 数据。您应该尝试 HttpPost 方法发送数据。然后,将您的数据放入请求正文中,您的控制器应该是:

[HttpPost]
public JsonResult TestGet([FromBody]IList<Contact> contactcollection)
{
    int ddd = 23;

    return Json(null);
}

【讨论】:

  • 我尝试了您的解决方案,但它不起作用。我也从前端的 Get 更改为 Post
  • 我忘记添加 [Frombody] 属性。请重新测试。
  • 我再次尝试了您的解决方案,但它不起作用。我也从前端的 Get 更改为 Post
【解决方案2】:

我想我明白这里发生了什么。你告诉你的控制器期待一个IEnumerable&lt;Contact&gt;,但你实际上只是传递了一个Contact。将您的 JSON 字符串更改为

var txt = '{"contactcollection":[{"name":"John", "age":30, "city":"New York"}]}'
var obj = JSON.parse(txt);

然后将您的 ajax 更改为:

$.ajax({
        url: '@Url.Action("TestGet")',
        data:obj,
        dataType: 'json',
        type: 'Post',
        contentType: 'application/json',
        success: function (result) {

            var display = '';


            return;
        }
    });

并使用 POST 而不是 GET

【讨论】:

    【解决方案3】:
    var values = ['1', '2', '3'];
    
    // Make the ajax call
    $.ajax({
        type: "POST",
        url: '@Url.Action("done")',
        data: { ids: values },
        dataType: "json",
        success: function (result) {
            alert('Yay! It worked!');
        },
        error: function (result) {
            alert('Oh no :(');
        }
    });
    

        [HttpPost]
        public JsonResult done(List<string> ids)
        {
            bool data = false;
    
            if (data)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json("The attached file is not supported", MediaTypeNames.Text.Plain);
            }
            else
            {
                Response.StatusCode = (int)HttpStatusCode.OK;
                return Json("Message sent!", MediaTypeNames.Text.Plain);
            }
        }
    

    来源

    AJAX Post of JavaScript String Array to JsonResult as List<string> Always Returns Null?

    https://www.c-sharpcorner.com/article/asp-net-mvc-how-to-use-ajax-with-json-parameters/

    Sending a javascript array to code behind(c#) using ajax

    Jquery Ajax, return success/error from mvc.net controller

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2017-12-06
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 2011-06-13
      相关资源
      最近更新 更多