【发布时间】:2015-01-21 03:17:35
【问题描述】:
for 循环中的数组返回 null。
当我这样做时:
abc.a = "1";
abc.b = "1";
abc.c = "1";
一切都好。 但这会返回 null:
for (var i = 0; i < 3; i++) {
abc[i] = "1";
}
Mvc 的控制器:
public class abcController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(abc val)
{
return View();
}
}
模型 mvc abcCLASS:
public class abc
{
public string a {get;set;}
public string b { get; set; }
public string c { get; set; }
}
HTML + Jquery
我希望 for 也能正常工作。
为什么在for循环中对象返回null?
<input type="button" id="bbb"/>
<script>
var abc = { a: "", b: "", c: "" };
$("#bbb").click(function () {
// This not working:
for (var i = 0; i < 3; i++) {
abc[i] = "1";
}
// This working:
abc.a = "1";
abc.b = "1";
abc.c = "1";
$.ajax({
url: '/abc/index',
type: "POST",
dataType: 'json',
traditional: true,
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(abc),
success: function (response) {
console.log(response);
}
});
});
</script>
【问题讨论】:
-
是什么让您认为
abc[0] = "1"与abc.a = "1";相同? -
请不要对非引用的内容使用引用格式。
-
for循环如何可能工作 - 它生成一个数组。您发回接受对象而不是集合的控制器方法! -
它适用于 webforms,mvc 不同
标签: javascript jquery json asp.net-mvc-5