$.getJSON returns array,help needed with $.each - json

i have this code in my view which is in a loop which gives me two rows for 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") %>
in my controller:
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);
}
each console.log gives:
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"}]}
how can i get the interactiontype,name and points seperate?
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
{
}

Simply use the $.each() function:
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 ...
});
});
Here we are looping over the data.result collection in which each element represents an object having InteractionType, Points and Name properties according to the log you have shown. The $.each will obviously be executed for each element of the result collection.
UPDATE:
After the small discussion we had in the comments section it seems that you are doing something fundamentally wrong here. You are attempting to pass to a server side helper values that you have retrieved on the client using AJAX. That's impossible nr it makes any sense.
So you should be binding on the server. You shouldn't be doing any AJAX requests at all. You should simply call your server side helper and pass it the required parameters:
<%: Html.GetQTip(
"training-module-id-" + module.TrainingModuleId,
Model.Points,
"training-module-id-" + module.TrainingModuleId,
Zinc.Web.Extensions.QTipPosition.Bottom,
true,
"Module Points"
) %>
Now all you have to do is add this Points property to your view model:
public string Points { get; set; }
and inside the controller action that is rendering this view simply set this property. You would first query your data layer to retrieve an IEnumerable<UserTrainingPointsDataModel> and then perform some transformation on this array to convert it to a string that you want to be displayed:
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);
Remark: I don't know where you took this Html.GetQTip helper but looking at its source code I am horrified. This helper doesn't encode anything. Your site is vulnerable to XSS attacks. Never use any string concatenations to build up javascript and pass variables to functions.

Related

Umbraco - MediaPicker Object Data to JSON

In Umbraco 7, is it possible to serialize a mediapicker to json? So it could be something like this....
[{'name':'muffin', 'file':'muffin.jpg', 'text':'some text', 'etc': 'and so on'}]
My setup is like this, I have a mediaPicker named "mediaPhotos". Contained in the folder selected by the mediaPicker I have images of a custom media type "sillyImage".
I can create a controller and query only single items as xml. What I'm trying to target the entire folder of images chosen by the mediapicker and convert its contents to json.
I'm trying to use the solution posted by bowserm below which works like this...
It gets the CurrentPage dynamically with the mediaPicker alias. Then its passed the custom media type.
public class MediaApiController : UmbracoApiController
{
[HttpGet]
public MediaApiModel GetMediaById(string id)
{
var media = Umbraco.TypedMedia(id);
return new MediaApiModel
{
MediaId = media.Id,
MediaUrl = media.Url
};
}
[HttpGet]
public IEnumerable<MediaApiModel> GetMediaObj(string mediaAlias)
{
var currentPage = Umbraco.TypedContent(UmbracoContext.Current.PageId);
var mediaRootId = currentPage.GetPropertyValue<string>("mediaPhotos");
var mediaRoot = Umbraco.TypedMedia(mediaRootId);
var media = mediaRoot.Children.Where(m => m.IsDocumentType(mediaTypeAlias));
return media.Select(m => new MediaApiModel
{
MediaId = m.Id,
MediaUrl = m.Url
});
}
}
var uri3 = '//' + document.domain + ':14712' + '/umbraco/api/MediaApi/GetMediaFolder?mediaAlias=sillyImage';
$(document).ready(function () {
$.getJSON(uri3)
.done(function (data) {
console.log('return json data object ' + data);
});
});
I'm getting a 500 error now so its getting closer. The issue I think is with these lines in the controller
var currentPage = Umbraco.TypedContent(UmbracoContext.Current.PageId);
var mediaRootId = currentPage.GetPropertyValue<string>("mediaPhotos");
var mediaRoot = Umbraco.TypedMedia(mediaRootId);
My pages use a page name so PageId I'm not sure is the root issue. The one item I know is that the GetPropertyValue isn't able to get the media picker object from the current page.
Thanks!
You should be able to get your Api Controller to automatically serialize the results to JSON. Just inherit from UmbracoApiController.
public class MediaApiController : UmbracoApiController
{
[HttpGet]
public MediaApiModel GetMediaById(string id)
{
var media = Umbraco.TypedMedia(id);
return new MediaApiModel
{
MediaId = media.Id,
MediaUrl = media.Url
};
}
[HttpPost]
public IEnumerable<MediaApiModel> GetMediaObj(string mediaTypeAlias)
{
var currentPage = Umbraco.TypedContent(UmbracoContext.Current.PageId);
var mediaRootId = currentPage.GetPropertyValue<string>("mediaPhotos");
var mediaRoot = Umbraco.TypedMedia(mediaRootId);
var media = mediaRoot.Children.Where(m => m.IsDocumentType(mediaTypeAlias));
return media.Select(m => new MediaApiModel
{
MediaId = m.Id,
MediaUrl = m.Url
});
}

Nancy OnError will not accept a Response object?

The Nancy documentation seems to say that Pipelines.OnError should return null - as opposed to BeforeResponse which allows both null and a Response object.
All the examples like this one and many code samples here on StackOverflow show a Response being returned in the OnError, just like in the BeforeRequest.
When I attempt to return an HTTPStatus string for the Pipelines.OnError, everything works OK!
But when I attempt to return a Response, I get a compiler error:
Operator '+=' cannot be applied to operands of type 'Nancy.ErrorPipeline' and 'lambda expression'
I'm emulating almost exactly the code in the Nancy example, except for the fact that mine is a TinyIocContainer while the example's is using a StructureMap container and a StructureMap derived bootstrapper
Here's my code:
const string errKey = "My proj error";
const string creationProblem = "Message creation (HTTP-POST)";
const string retrievalProblem = "Message retrieval (HTTP-GET)";
public void Initialize(IPipelines pipelines)
{
string jsonContentType = "application/json";
byte[] jsonFailedCreate = toJsonByteArray(creationProblem);
byte[] jsonFailedRetrieve = toJsonByteArray(retrievalProblem);
Response responseFailedCreate = new Response
{
StatusCode = HttpStatusCode.NotModified,
ContentType = jsonContentType,
Contents = (stream) =>
stream.Write(jsonFailedCreate, 0, jsonFailedCreate.Length)
};
Response responseFailedRetrieve = new Response
{
StatusCode = HttpStatusCode.NotFound,
ContentType = jsonContentType,
Contents = (stream) =>
stream.Write(jsonFailedRetrieve, 0, jsonFailedRetrieve.Length)
};
// POST - error in Create call
pipelines.OnError += (context, exception) =>
{
// POST - error during Create call
if (context.Request.Method == "POST")
return responsefailedCreate;
// GET - error during Retrieve call
else if (context.Request.Method == "GET")
return responseFailedRetrieve;
// All other cases - not supported
else
return HttpStatusCode.InternalServerError;
};
}
private byte[] toJsonByteArray(string plainString)
{
string jsonString = new JObject { { errKey, plainString } }.ToString();
byte[] result = Encoding.UTF8.GetBytes(jsonString);
return result;
}
I had the same problem and I found a nice approach to the problem: http://paulstovell.com/blog/consistent-error-handling-with-nancy.
you should override RequestStartup on the Bootstrapper, here my test code:
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
pipelines.OnError.AddItemToEndOfPipeline((ctx, ex) =>
{
DefaultJsonSerializer serializer = new DefaultJsonSerializer();
Response error = new JsonResponse(ex.Message,serializer);
error.StatusCode = HttpStatusCode.InternalServerError;
return error;
});
base.RequestStartup(container, pipelines, context);
}

View gets Json displayed on the page. not the data

I have an action which returns a JsonResult. The only thing gets displayed on the view is my json which is like
ProcessOrder{"IsValid":true,"url":"/Home/ProcessOrder"}
While debugging the code, I noticed that it gets displayed because of this below line.
var ProcessOrderData = new { IsValid = true, url = Url.Action("ProcessOrder") };
return new JsonResult() { Data = ProcessOrderData };
Can any body please tell me why it gets only json to be displayed on the view?
is something null here that is causing this to get this displayed or any other stuff?
Code:
private ActionResult SubmitAccount(UserAccountModels UserAccountModels)
{
SessionInfo userSession = SiteSetting.Visitor;
if (userSession != null)
{
if (userSession.products.Where(rec => rec.IsAddedToCart).Count() > 0)
{
SiteSetting.Visitor.User.FirstName = UserAccountModels.FirstName;
SiteSetting.Visitor.User.LastName = UserAccountModels.LastName;
SiteSetting.Visitor.User.Phone = UserAccountModels.Phone;
SiteSetting.Visitor.User.Email = UserAccountModels.Email;
var ProcessOrderData = new { IsValid = true, url = Url.Action("ProcessOrder") };
return new JsonResult() { Data = ProcessOrderData };
}}}
It will only display Json because you are returing JsonResult not a View

Unable to sort Dgrid

var CustomGrid = declare([Grid, Keyboard, Selection]);
var questionGrid = new CustomGrid({
store: questionCacheStore,
columns: [
editor({
label: "Questions",
field: "question",
editor: "text",
editOn: "dblclick",
sortable:true})
],
selectionMode: "single",
cellNavigation: false
}, "questions");
I am new to Dgrid. So, please do bear with me .
i was able to populate the dgrid with a JsonStore content. But when i click on the column 'Questions', it doesn't get sorted as in local data store.instead it shows an error Uncaught TypeError: Object [object Object] has no method 'sort'. Is it required to define such a method . If so, how and where should i define it ?
I am not the person to answer your J2EE question. I asked that question recently. The solution that I found was to inject the HttpServletRequest directly. This allowed me access to the query string parameters. From there I was able to get the sort direction (ascending, descending) and column to sort. Hopefully the snippets below will help.
Example Grid Setup
require(["dojo/store/JsonRest", "dojo/store/Memory", "dojo/store/Cache",
"dojo/store/Observable", "dgrid/OnDemandGrid", "dojo/_base/declare", "dgrid/Keyboard",
"dgrid/Selection", "dojo/domReady!"],
function(JsonRest, Memory, Cache, Observable, Grid, declare, Keyboard, Selection) {
var rest = new JsonRest({target:"/POC_Admin/rest/Subcategory/", idProperty: "subcatId"});
var cache = new Cache(rest, new Memory({ idProperty: "subcatId" }));
var store = new Observable(cache);
var CustomGrid = declare([ Grid, Keyboard, Selection ]);
var grid = new CustomGrid({
columns: {
subcatId: "ID",
name: "Name"
},
store: store
}, "grid");
grid.on("dgrid-select", function(event){
// Report the item from the selected row to the console.
console.log("Row selected: ", event.rows[0].data);
});
grid.startup();
});
Example Rest GET
#Context private HttpServletRequest servletRequest;
#GET
#Path("")
#Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
public String getSubcategories(#QueryParam("name") String name) throws IOException {
//Respond to a QueryString value.
if (servletRequest.getQueryString() != null && servletRequest.getQueryString().length() > 0) {
String querystringKey = servletRequest.getQueryString();
System.out.println("QSKey = " + querystringKey);
System.out.println("Substr: " + querystringKey.substring(0, 4));
if (querystringKey.length()>4) {
if (querystringKey.substring(0, 4).contains("sort")) {
//We have the sort request.
}
}
}
//Return all results otherwise from your DAO at this point
}

Working with JSON data.. why are all properties of my object undefined?

I am attempting to take the Name and ID fields from each object, but the fields are appearing undefined.
function OnHistoricalListBoxLoad(historicalListBox) {
$.getJSON('GetHistoricalReports', function (data) {
historicalListBox.trackChanges();
$.each(data, function () {
var listBoxItem = new Telerik.Web.UI.RadListBoxItem();
listBoxItem.set_text(this.Name);
listBoxItem.set_value(this.ID);
historicalListBox.get_items().add(listBoxItem);
});
historicalListBox.commitChanges();
});
}
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetHistoricalReports()
{
List<HistoricalReport> historicalReports = DashboardSessionRepository.Instance.HistoricalReports;
var viewModel = historicalReports.Select(report => new
{
ID = report.ID,
Name = report.Name
}).ToArray();
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
I know that I am returning the data successfully, and I know that there is valid data. I am new to MVC/JavaScript, though.. I checked case sensitivity to ensure that I wasn't making just an easy mistake, but it does not seem to be the issue. Am I missing something more complex?
Inspecting the HTTP Response JSON tab in Chrome I see:
0: {ID:1, Name:PUE}
1: {ID:2, Name:Weight}
2: {ID:3, Name:Power Actual vs Max}
3: {ID:4, Name:Power Actual}
No idea, but passing such behemoth domain models to views is very bad practice. This is so kinda domain polluted that it has nothing to do in a view. In a view you work with views models. View models contain only what a view needs. In this case your view needs an ID and a Name. So pass a view model with only those single simple properties to this view:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetHistoricalReports()
{
var reports = DashboardSessionRepository.Instance.HistoricalReports;
var reportsViewModel = reports.Select(x => new
{
ID = x.ID,
Name = x.Name
}).ToArray();
return Json(reportsViewModel, JsonRequestBehavior.AllowGet);
}
Now, not only that you will save bandwidth, but you will get some clean JSON:
[ { ID: 1, Name: 'Foo' }, { ID: 2, Name: 'Bar' }, ... ]
through which you will be able to loop using $.each.
UPDATE:
Now that you have shown your JSON data it seems that there is a Content property which represents the collection. So you need to loop through it:
$.each(data.Content, ...);
and if you follow my advice about the view models your controller action would become like this:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetHistoricalReports()
{
var report = DashboardSessionRepository.Instance.HistoricalReports;
var reportsViewModel = report.Content.Select(x => new
{
ID = x.ID,
Name = x.Name
}).ToArray();
return Json(reportsViewModel, JsonRequestBehavior.AllowGet);
}
and now loop directly through the returned collection:
$.each(data, ...);