How to use subqueries with Breeze.js - javascript

We have two entities, User and Person.
public class User
{
public int Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int PersonId { get; set; }
public Person Person { get; set; }
}
and
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Every user has a person, but not every person has a user.
I would like to retrieve all the persons, that aren't assigned to a user.
Is there a way to do this using breeze.js?
I can't find anything on using subqueries with Breeze, but I imagine there should be some sort of 'in' clause or some way to use/make a subquery

Updated post: 11/25/13
As of Breeze 1.4.6, 'any' and 'all' operators are now supported.
Older post
Right now there is no easy way to do this. We do plan to support 'any' and 'all' query filters in the future but we just haven't gotten to it yet. Please vote for this here.
As a workaround, in some scenarios you can query for the value of the foreign key being null or 0 but I don't think that this will work in your case, given your schema.

Related

Converting MVC Model List to JSLINQ Error

I have three dropdownlist BodyPart, ExamDetail and ExamView and I have complete data set for these lists. I don't need to call controller again and again whenever the dropdown change event call but I want to fetch list from my model's property list. I am using JS Library to apply LINQ Queryto avoid any loops in code.
My Class Architecture is given below:
public class BodyPart
{
public string ID { get; set; }
public string Text { get; set; }
public List<ExamDetail> examdetail { get; set; }
}
public class ExamDetail
{
public string ID { get; set; }
public string Text { get; set; }
public List<ExamView> examview { get; set; }
}
public class ExamView
{
public string ID { get; set; }
public string Text { get; set; }
}
I have proper data in all my list, here is my js code to fetch ExamDetail Record
var selectedBodyPart = $("#BodyPartDDL").val();
var examdetailList = JSLINQ(#Model.bodypart).Where(function (item) {
return item.ID == selectedBodyPart; });
But I am getting "Uncaught SyntaxError: Unterminated template literal(…)" error where I pass my model list. I need to pass this examdetailList to my ExamDetail partial view. Thank you.

Editing a single entity across different controller views w/out calling db with each action?

I have an object/entity called 'Space'. I want to put each of the different entities (images, details, location, etc) inside 'Space' onto different pages/views so that each part can be edited separately, the user clicks on nav links to edit each section. I don't want to have to fetch the entity from the database every time it posts back to another action. Isn't there a better way to do this?
Maybe having each one of these as action methods (location, images, details) isn't the correct approach? Is hiding different parts of the page better with javascript? Please advise! I'm trying to model it after airbnb's page that edits a listing...
Ex.
mysite/managespaces/123/images
// you see the form to edit pics for id 123
mysite/managespaces/123/location
//here you only edit loc details for id 123
mysite/managespaces/123/details
//here details are edited for id 123
etc...
public class Space
{
public int SpaceId { get; set; }
[Index(IsUnique = false)]
[Required]
[MaxLength(128)]
public string AspNetUserRefId { get; set; }
public virtual SpaceOverview Overview { get; set; }
public virtual SpaceDetails Details { get; set; }
public virtual ICollection<SpaceImage> Images { get; set; }
public virtual SpaceListing Listing { get; set; }
public virtual SpaceAddress Location { get; set; }
public virtual ICollection<SpaceReview> Reviews { get; set; }
[Required]
public DateTime DateCreated { get; set; }
[Required]

How to represent the value reference to my lookup data in my ViewModel?

I have a list of (for example) countries in my database, which might have a class representation that looks like;
public class Country {
public int CountryId { get; set; }
public string CountryName { get; set; }
}
Now i have a ViewModel class that models a Student for example, and it looks like;
public class StudentViewModel {
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
When i want to provide the Country the Student comes from, i usually include the following properties in the StudentViewModel class;
public int CountryId { get; set; }
public string CountryName { get; set; }
Now, i not quite sure if i am doing the right thing because on the client (using javascript), i have to keep the CountryId and CountryName synchronized. The reason i have done it this way for a while is because i used the CountryId property to set the initial value of the dropdownlist and CountryName to show the textual value on a different section of the same form.
I would like to know how other people are handling situations like this?

Unable to send JSON data to MVC controller

I have a JavaScript function that looks as follows:
function exportToExcel() {
$.ajax({
url: "/eBird/ExportToExcel",
data: jsonSightingData,
type: 'POST',
contentType: 'application/json'
});
}
My MVC controller looks like this:
public ActionResult ExportToExcel(List<Entities.MyClass> data)
{
try
{
...
}
catch (System.Exception exception)
{
...
}
MyClass defintion is:
public class MyClass
{
public string comName { get; set; }
public int howMany { get; set; }
public double lat { get; set; }
public double lng { get; set; }
public string locID { get; set; }
public string locName { get; set; }
public bool locationPrivate { get; set; }
public string obsDt { get; set; }
public bool obsReviewed { get; set; }
public bool obsValid { get; set; }
public string sciName { get; set; }
}
The class matches the JSON data coming in exactly. The problem is that when my controller method is called, 'data' is always NULL. My understanding was that the MVC model binder would automatically bind the JSON data to my MyClass list. But it doesn't appear to be working.
Sample JSON is as follows:
[{"comName":"Great Black-backed Gull","lat":42.4613266,"lng":-76.5059255,"locID":"L99381","locName":"Stewart Park","locationPrivate":false,"obsDt":"2014-09-19 12:40","obsReviewed":false,"obsValid":true,"sciName":"Larus marinus"}]
Use a General Object (Like JContainer) to "capture" the incoming data. or even Object if you are not sure what you get.
Then see what you have inside.
I use an external deserializer to convert json back to class. (using .ToString() to make it readable to the serializer)
(try Json.Net)
Also - make sure that the JSON is not converted into Unicode. I've had an issue with that as well.
another remark - I think content type should be application/json.
Read this: SO about json content type
Run Fiddler and capture Json sent to controller. The controller expects a List but looks like you are sending single object. Stuff coming in as null happens to many of us. I'll try and run what you have. Can you also mess with controller and give it just MyClass coming in?

bind JSON list in array to ASP.NET model class

I am building JSON output from my array that I am intended to pass back to server where I have model class to bind JSON data variable to class variables. In this class I am also taking multiple records of say for argument 'Component' and to bind this part I have IList in my model class.
Now I have managed to pass data back to controller except the Components that is in IList... I am struggling to find answer.. your help will be really appreciated..
Model class
public class QualificationElementComponents_ViewModel
{
public int ElementIndex { get; set; }
public string ElementMarkingSchemeTitle { get; set; }
public int ElementAvailableMark { get; set; }
public int ElementPassMark { get; set; }
public int ElementMeritMark { get; set; }
public int ElementDistinctionMark { get; set; }
public IList<ECom1> ElementComponent { get; set; }
}
IList 'Component' Model class
public class ECom1
{
public int componentIndex { get; set; }
public int componentMark { get; set; }
}
Controller Method
public ActionResult CreateNewQualification(QualificationViewModel newQualificationData, IList<QualificationElementComponents_ViewModel> ElementComponentList)
{
in view
//build component list... possible will have multiple records in array
selectedComponentList.push({ componentIndex: recordId, componentMark: ComponentSchemeMark });
// build element list
selectElementList.push({ ElementIndex: E_RecordId, ElementMarkingSchemeTitle: E_MarkingSchemeTitle, ElementAvailableMark: E_AvailableMark, ElementPassMark: E_PassMark, ElementMeritMark: E_MeritMark, ElementDistinctionMark: E_DistinctionMark });
//bind arrays
selectElementList.push({ ElementComponent: selectedComponentList });
QualificationElemenetsAndComponentsList.push.apply(QualificationElemenetsAndComponentsList, selectElementList);
JSON Output
{"QualificationElemenetsAndComponentsList":[{"ElementIndex":1,"ElementMarkingSchemeTitle":"fg","ElementAvailableMark":"56","ElementPassMark":"6","ElementMeritMark":"5","ElementDistinctionMark":"6"},{"ElementComponent":[{"componentIndex":1,"componentMark":"23"},{"componentIndex":2,"componentMark":"44"}]}]}
require JSON Output
in comparison to above JSON I require following JSON format
{"QualificationElemenetsAndComponentsList":[{"ElementIndex":1,"ElementMarkingSchemeTitle":"d2","ElementAvailableMark":"223","ElementPassMark":"32","ElementMeritMark":"12","ElementDistinctionMark":"2","ElementComponent":[{"componentIndex":2,"componentMark":551}]}]}
Instead of adding the ElementComponent property to a new object and then into the array, you need to include it with the other properties like so:
//build component list... possible will have multiple records in array
selectedComponentList.push({ componentIndex: recordId, componentMark: ComponentSchemeMark });
// build element list
selectElementList.push({ ElementIndex: E_RecordId, ElementMarkingSchemeTitle: E_MarkingSchemeTitle, ElementAvailableMark: E_AvailableMark, ElementPassMark: E_PassMark, ElementMeritMark: E_MeritMark, ElementDistinctionMark: E_DistinctionMark, ElementComponent: selectedComponentList });
//Add ElementComponent with all the other properties

Categories

Resources