【发布时间】:2020-03-02 04:39:44
【问题描述】:
使用数组参数向控制器提交 POST ajax 调用。
我有一个参数数组,
我有一个静态数组,我用它来检查参数数组。
我使用 .Except 方法创建了第三个数组,以创建一个除参数值之外的所有内容的数组。
POST ajax 调用正常工作。我可以返回并查看我发送给它的值。这就是我用那个快速的 TempData 所做的。所以,我知道参数不为空。
这里是控制器:
[HttpPost]
public ActionResult MyAction(string[] subLineNames)
{
//Static array to check against parameter
string[] sublineArray = new string[] { "BI/PD", "Hired", "Non-Owned", "PIP", "Addtl-PIP", "Medical Payments", "UM PD", "UM CSL", "UIM CSL", "Terrorism" };
//Create new array for all minus the values in the parameter
/* The error happens here. The .Trim is causing some issue I can't see. */
/* I know that jquery ajax call is sending a bunch of white space, so I use trim to get rid of the white space characters. */
string[] DifferArray = sublineArray.Except(subLineNames.Select(m => m.Trim())).ToArray();
//Test to ensure the array parameter is not empty. (it works and brings back what I sent to it)
if (subLineNames != null)
{
for (int i = 0; i < subLinesNames.Length - 1; i++)
{
TempData["AA"] += subLineNames[i];
}
}
}
令人沮丧,因为我之前有这个工作。我没有改变任何会导致它现在这样做的东西。任何帮助将不胜感激。
【问题讨论】:
-
@swapmeet_Lou 你想通过你的 ajax 完成什么?你想改变视图吗?您正在从控制器返回一个字符串
"Testing"。 -
发生在哪一行和哪个变量上?可能 subLineNames 为空
-
@Ryan Wilson。我将这些值提交到我设置的表中。我把它排除在控制器之外,因为这不是问题所在。另外,如果我不能让它以这种简单的方式工作,我就不能将它们插入表中。这实际上是一周前的工作,而我所做的事情导致这搞砸了。我只是不明白为什么。即使在这个最基本的水平
-
如果
subLineNames的元素包含null值,那么您尝试在其上调用.Trim().. ker-pow。没有看到实际值,就无法确定。我会在此方法上设置一个断点并检查实际存在的内容..然后将其发布在这里,以便我们提供更多帮助。最后,您的操作方法不会返回任何内容。
标签: c# jquery asp.net-mvc model-view-controller