我在我的视图中有这个代码,它在循环中为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"}]}
如何才能获得interactiontype,name和points分开?
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
{
}
答案 0 :(得分: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并将变量传递给函数。