0

ビューにこのコードがあり、これはループにあり、console.log に 2 つの行が表示されます

var jqxhr = $.getJSON("<%= Url.Action("GetTrainingModulePoints" , "Home") %>", function (data) {
      console.log(JSON.stringify(data));
   });
   <%: Html.GetQTip("training-module-id-" + module.TrainingModuleId , "With assesment"  , "training-module-id-" + module.TrainingModuleId , Zinc.Web.Extensions.QTipPosition.Bottom, true, "Module Points") %>

私のコントローラーで:

public JsonResult GetTrainingModulePoints()
{
  var currentUser = ZincService.GetUserForId(CurrentUser.UserId);
  IEnumerable<DataModels.Training.UserTrainingPointsDataModel> modulePoints = ZincService.TrainingService.GetTrainingModulePoints(currentUser.UserId);
  return Json(new { result = modulePoints}, JsonRequestBehavior.AllowGet);
}

各 console.log は以下を提供します:

 LOG: {"result":[{"InteractionType":6,"Points":50,"Name":"China Incentive Program"},{"InteractionType":8,"Points":1,"Name":"China Incentive Program"},{"InteractionType":6,"Points":50,"Name":"India - Q2 Incentive "},{"InteractionType":8,"Points":100,"Name":"India - Q2 Incentive "},{"InteractionType":6,"Points":50,"Name":"China - Q2 Incentive"},{"InteractionType":8,"Points":3,"Name":"China - Q2 Incentive"}]} 

 LOG: {"result":[{"InteractionType":6,"Points":50,"Name":"China Incentive Program"},{"InteractionType":8,"Points":1,"Name":"China Incentive Program"},{"InteractionType":6,"Points":50,"Name":"India - Q2 Incentive "},{"InteractionType":8,"Points":100,"Name":"India - Q2 Incentive "},{"InteractionType":6,"Points":50,"Name":"China - Q2 Incentive"},{"InteractionType":8,"Points":3,"Name":"China - Q2 Incentive"}]} 

インタラクションの種類、名前、ポイントを別々に取得するにはどうすればよいですか?

public static MvcHtmlString GetQTip(this HtmlHelper htmlHelper, string propertyName, string message, string propertyNameOverride = "", QTipPosition position = QTipPosition.Right, bool includeEvents = true, string title = "")
{
  string qtipPosition = String.Empty;

  switch (position)
  {
    case QTipPosition.Right:
      qtipPosition = "my: 'left center', at: 'right center'";
      break;
    case QTipPosition.Left:
      qtipPosition = "my: 'right center', at: 'left center'";
      break;
    case QTipPosition.Top:
      qtipPosition = "my: 'top middle', at: 'bottom middle'";
      break;
    case QTipPosition.Bottom:
      qtipPosition = "my: 'bottom middle', at: 'top middle'";
      break;
  }

  if (!String.IsNullOrWhiteSpace(propertyNameOverride))
    propertyName = propertyNameOverride;

  if (String.IsNullOrWhiteSpace(title))
    title = htmlHelper.Resource(Resources.Global.Title.Information);

  StringBuilder sb = new StringBuilder();
  sb.Append(String.Concat("$('#", propertyName, "').removeData('qtip').qtip({content: {text:"));
  sb.Append(String.Concat("'", message, "', title: { text: '", title, "', button: false }}, position: { ", qtipPosition, " }"));
  if (includeEvents)
    sb.Append(", show: { event: 'focus mouseenter', solo: true, ready: false }, hide: 'blur'");
  sb.Append(", style: { classes: 'ui-tooltip-shadow ui-tooltip-yellow' } });");

  return new MvcHtmlString(sb.ToString());
  }
}

 public sealed class MvcHtmlString : HtmlString
 {

 }
4

1 に答える 1

1

$.each()次の関数を使用するだけです。

var url = '<%= Url.Action("GetTrainingModulePoints" , "Home") %>';
var jqxhr = $.getJSON(url, function (data) {
    $.each(data.result, function() {
        var interactionType = this.InteractionType;
        var name = this.Name;
        var points = this.Points;
        // do something with those variables ...
    });
});

ここでは、あなたが示したログによると、各要素が、およびプロパティdata.resultを持つオブジェクトを表すコレクションをループしています。結果コレクションの各要素に対して実行されることは明らかです。InteractionTypePointsName$.each


アップデート:

コメント セクションで行った小さな議論の後、ここで根本的に間違ったことをしているようです。AJAX を使用してクライアントで取得したヘルパー値をサーバー側に渡そうとしています。それは不可能であり、意味がありません。

したがって、サーバーにバインドする必要があります。AJAX リクエストをまったく実行しないでください。サーバー側のヘルパーを呼び出して、必要なパラメーターを渡すだけです。

<%: Html.GetQTip(
    "training-module-id-" + module.TrainingModuleId, 
    Model.Points, 
    "training-module-id-" + module.TrainingModuleId, 
    Zinc.Web.Extensions.QTipPosition.Bottom, 
    true, 
    "Module Points"
) %>

Pointsあとは、このプロパティをビュー モデルに追加するだけです。

public string Points { get; set; }

このビューをレンダリングするコントローラー アクション内で、このプロパティを設定するだけです。最初にデータ レイヤーにクエリを実行して を取得しIEnumerable<UserTrainingPointsDataModel>、次にこの配列に対して何らかの変換を実行して、表示する文字列に変換します。

MyViewModel model = ... get the view model from somewhere
var currentUser = ZincService.GetUserForId(CurrentUser.UserId);
var modulePoints = ZincService.TrainingService.GetTrainingModulePoints(currentUser.UserId);
model.Points = ... transform the original points collection to some string that you want to pass to the helper;
return View(model);

注意: このヘルパーをどこに持って行ったのかわかりませんがHtml.GetQTip、そのソース コードを見るとぞっとします。このヘルパーは何もエンコードしません。あなたのサイトは XSS 攻撃に対して脆弱です。文字列の連結を使用して JavaScript を作成し、変数を関数に渡すことは絶対にしないでください。

于 2013-01-02T11:01:03.257 に答える