[英]create array in javascript with Html.action
I'm trying to create an array of strings in javascript by calling a function in my MVC controller and passing back an array of strings. 我试图通过在MVC控制器中调用一个函数并传回字符串数组来在javascript中创建字符串数组。 This pretty simply just isn't working and I'm not sure what i need to do to amend this.
这完全是行不通的,我不确定我需要做些什么来对此进行修改。 below you can see both my javascript and controller code.
在下面,您可以看到我的JavaScript和控制器代码。 Any help is greatly appreciated
任何帮助是极大的赞赏
javascript: JavaScript的:
var optionString = @Html.Action("PopulateDashboardDropdown", "Embed", new { Dashboards = Model[0][0].Dashboards });
Controller: 控制器:
public string[] PopulateDashboardDropdown(ODataResponseListDashboard[] dashboards)
{
string email = "";
//loop that finds the groupID assigned to the currently logged in user
foreach (Claim claim in ClaimsPrincipal.Current.Claims)
{
if (claim.Type == "emails")
{
email = claim.Value;
email = email.ToLower();
}
}
string[] orgs = GetOrgs(email);
string[] retVal = new string[orgs.Length];
bool[] admin = new bool[orgs.Length];
for (int i = 0; i < orgs.Length; i++)
{
admin[i] = isAdmin(orgs[i], email);
retVal[i] = "";
}
//loop that creates a string to emulate the innerHtml of a dropdown selector based on the names of all dashboards in the workspace
for (int i = 0; i < orgs.Length; i++)
{
for (int j = 0; j < dashboards[i].Value.Count; j++)
{
if (dashboards[i].Value.ElementAtOrDefault(j).DisplayName.Contains("Admin"))
{
if (admin[i])
{
retVal[i] += "<option>" + dashboards[i].Value.ElementAtOrDefault(j).DisplayName + "</option>";
}
}
else
{
retVal[i] += "<option>" + dashboards[i].Value.ElementAtOrDefault(j).DisplayName + "</option>";
}
}
}
return retVal;
}
Well, from isn't clear what is the error or what happens exactly but I see a couple of problems. 好吧,从不清楚是什么错误或到底发生了什么,但是我看到了几个问题。 I suggest the following in order to fix the code:
我建议以下内容以修复代码:
1) Change the result of your controller method to JsonResult: 1)将控制器方法的结果更改为JsonResult:
public JsonResult PopulateDashboardDropdown(ODataResponseListDashboard[] dashboards)
{
...
return this.Json(retVal);
}
2) Get your data via an ajax call (note: you need to insert the proper url in http format. The Http.Action/Razor will not work in javascript): 2)通过ajax调用获取数据(注意:您需要以http格式插入正确的url。Http.Action / Razor在javascript中不起作用):
$.getJSON(myUrl, function (data) {
var optionString = data;
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.