0

我在我的视图中有这段代码,它在一个循环中,它为我提供了两行 console.log

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集合,其中每个元素代表一个对象,根据您显示的日志,该对象具有InteractionType,Points和属性。Name显然将$.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 回答