在MVC 4中,如何将View中的JavaScript数组传递给使用AJAX的Controller中的函数?
这似乎不起作用:
$.ajax(
{
type: "POST",
url: "../Home/SaveTable",
data: { function_param: countryArray }
});
Run Code Online (Sandbox Code Playgroud)
问题是,countryArray是JavaScript视图中的全局数组,我在传递之前检查它是否包含元素.但是,当saveTable函数接收到数组时,该函数表示它收到了一个空字符串[]数组.
我只知道将数组从Controller传递给View,您可以将复杂数据类型序列化return Json(data, JsonRequestBehavior.AllowGet);,然后通过将其设置为"var"变量对其进行反序列化.
所以我可能也必须这样做,但如何?
编辑1:
这是SaveTable函数的缩短版本:
public string SaveTable(string[] function_param)
{
if (function_param != null && function_param > 0)
{
//some code
return "Success";
}
//The following code will run if it's not successful.
return "There must be at least one country in the Region.";
//Yeah it's always returning this b/c function_param is null;
}
Run Code Online (Sandbox Code Playgroud) 当我从ActionResult函数创建强类型视图时,用于创建视图的Visual Studio对话框只允许我包含一个模型对象.
如何包含1个以上,以便我可以为所有这些使用智能?
这个项目有2个对象:Region和Area.
两个对象都有一个名为的方法
void load();
Run Code Online (Sandbox Code Playgroud)
这就是我想要的,不确定它是否可能:
Detail根据称为函数的对象调用具有类似实现的相同函数.
该Detail函数将执行以下操作:
void Detail(parameter)
{
object_name.load();
}
Run Code Online (Sandbox Code Playgroud)
我不想为每个对象编写2个重载函数,因为那时我将有2个函数具有几乎相同的实现.
我试过了:
void Detail(string land)
{
if(land=="region")
{
Region land = new Region();
}
else if(land=="area")
{
Area land = new Area();
}
land.load();
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为land.load()会导致错误,因为函数无法确定土地是一个Region还是一个Area对象.
所以我知道查询符号
var word = from s in stringList
where s.Length == 3
select s;
Run Code Online (Sandbox Code Playgroud)
相当于点符号
var word = stringList
.Where(s => s.Length == 3)
.Select(s => s);
Run Code Online (Sandbox Code Playgroud)
但是,如何将此点符号转换为查询符号?
var word = wordsList
.Single(p => p.Id == savedId);
Run Code Online (Sandbox Code Playgroud)
我在Google上找不到太多资源.
创建匿名数组的语法规则是什么?
即
int[] function()
{
int element1 = 1;
int element2 = 2;
return //array with element1 and element2
{
Run Code Online (Sandbox Code Playgroud)
它有什么其他用途?
c# ×4
asp.net-mvc ×3
ajax ×1
function ×1
javascript ×1
json ×1
linq ×1
object ×1
overloading ×1