JavaScriptの文字列を作成しようとしています。MVCコントローラで関数を呼び出し、文字列の配列を戻します。これはかなり単純に機能していないし、私はこれを修正するために何をする必要があるのか分からない。下に私のjavascriptとコントローラのコードの両方を見ることができます。すべてのヘルプは大Html.actionでjavascriptで配列を作成する
JavaScriptを高く評価されています
var optionString = @Html.Action("PopulateDashboardDropdown", "Embed", new { Dashboards = Model[0][0].Dashboards });
コントローラー:
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;
}
これに '@ Html.Action()'を使用しているのはなぜですか?なぜ、 'JsonResult'を返すメソッドを呼び出すajaxを使用しないのですか? –
'$ .ajax'' JsonResult'を 'success'パーツで呼び出すと問題ありません。 'Html.Action'は、JSONではなく、ビュー全体または部分的なビューを返すアクションメソッドで使用するためのものです。 –
もう一つの方法があります:あなたのモデルや 'ViewBag'のリストプロパティとして値を取得します。次に、あなたのcshtmlでループスルーして、javascript配列を設定します: ' ' – adiga