【发布时间】: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