我想知道是否可以将 JSON 数据传递到使用 KeyValuePair 类型作为参数的 ASP.NET MVC 控制器方法中。我在控制器方法中的 stopper 变量上有一个断点。当我调试时,我有以下 x 和 y 值:
x = 0
y = 空
如何让 keyValuePair 变量从 JSON 对象中正确填充?
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '@Url.Action("KeyValuePairTest", "Home")',
data: JSON.stringify({
keyValuePair: {
Key: 1,
Value: 'some text'
}
})
});
});
</script>
using System.Collections.Generic;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public void KeyValuePairTest(KeyValuePair<int, string> keyValuePair)
{
var x = keyValuePair.Key;
var y = keyValuePair.Value;
var stopper = "stop";
}
}
}