检索 Json 数据作为 IEnumerable
Retrieve Json Data as a IEnumerable
目标:
从前端向后端发送一个包含许多数据的 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;
}
}
无法通过 HttpGet 发送 IEmunerable 数据。您应该尝试使用 HttpPost 方法发送 data.Then,将您的数据放入请求正文中,并且您的控制器应该是:
[HttpPost]
public JsonResult TestGet([FromBody]IList<Contact> contactcollection)
{
int ddd = 23;
return Json(null);
}
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
我想我明白这里发生了什么。你告诉你的控制器期待一个 IEnumerable<Contact>
,但你实际上只是传递了一个 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
目标:
从前端向后端发送一个包含许多数据的 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;
}
}
无法通过 HttpGet 发送 IEmunerable 数据。您应该尝试使用 HttpPost 方法发送 data.Then,将您的数据放入请求正文中,并且您的控制器应该是:
[HttpPost]
public JsonResult TestGet([FromBody]IList<Contact> contactcollection)
{
int ddd = 23;
return Json(null);
}
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
我想我明白这里发生了什么。你告诉你的控制器期待一个 IEnumerable<Contact>
,但你实际上只是传递了一个 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