I have this class:
public partial class VehicleSize
{
public VehicleSize()
{
}
public System.Guid VehicleSizeId { get; set; }
public int Title { get; set; }
public int SizeOrder { get; set; }
public System.DateTime DateCreated { get; set; }
public Nullable<System.DateTime> DateDeleted { get; set; }
}
I'm using with BreezeJS.
Handling of Nullable seems a bit odd. This field is meant to be optional. But it complains until I explicitly set DateDeleted = null from the javascript. If I don't set the field to null I'll get "Problem saving TypeError: entity.dateDeleted is null".
It seems if i inspect the .dateDeleted property in javascript it is a javascript Date object but getTime isNAN. Shouldn't it be null in this case instead and the BreezeJS validator skip validating it as the field is null.
The Meta data from the server defines the field as this:
"name": "DateDeleted",
"type": "Edm.DateTime",
"nullable": "true"
Does anyone have an idea on how I create an optional DateTime field?
Further to my original post. The problem seems to be related to exporting Entities from one entity manager to another. A Date field that starts out as "null" becomes "Invalid Date" after its imported again as demonstrated by this code:
var vehicleSizeId = 'E9DA5803-BB65-4751-AA22-17B54A1EE7C1';
alert('getting vehicle from db');
var query = breeze.EntityQuery
.from("VehicleSizes")
.where("vehicleSizeId", "==", vehicleSizeId);
var em = new breeze.EntityManager("api/Todo");
em.enableSaveQueuing(true);
var sandBoxEm = em.createEmptyCopy();
sandBoxEm.enableSaveQueuing(true);
em.executeQuery(query)
.then(function (data) {
alert('Found the vehicle size in the original entity manager')
var entity = data.results[0];
alert('Org Date Deleted == ' + entity.dateDeleted); // Its null at this point
var bundle = em.exportEntities();
sandBoxEm.importEntities(bundle, { mergeStrategy: breeze.MergeStrategy.OverwriteChanges });
});
sandBoxEm.executeQuery(query)
.then(function (data) {
alert('Found the vehicle size')
var entity = data.results[0];
alert('Date Deleted == ' + entity.dateDeleted); // Now its invalid date
entity.sizeOrder = entity.sizeOrder + 1;
entity.titleTranslation.text = entity.titleTranslation.text + "_x";
//entity.titleTranslation.dateDeleted = null;
//entity.dateDeleted = null;
try {
sandBoxEm.saveChanges().then(
function () {
alert('It saved ok');
}).fail(
function (error) {
var firstItem = error.entitiesWithErrors[0];
var firstError = firstItem.entityAspect.getValidationErrors()[0];
var msg = "prop: " + firstError.property.parentType.name + " = " + firstError.errorMessage;
alert(msg);
}
);
}
catch (ex) {
alert( "Problem saving " + ex );
}
}
).fail(
function () {
alert('Getting the vehicle size failed');
});
Edit: May 8, 2013 - The issue with importing a previously exported null date is now fixed in v 1.3.3 and available on the Breeze website.
Not sure what you are experiencing. I just wrote a unit test to try and confirm what you are seeing and are not able to repro your issue. I am running this against an Employee model where the "birthDate" also defined as a Nullable.
All tests pass on this:
var em = newEm(); // creates a new EntityManager
var emp = em.createEntity("Employee", { firstName: "Joe", lastName: "Smith" });
ok(emp.entityAspect.entityState === breeze.EntityState.Added, "entityState should be 'Added'");
var birthDate = emp.getProperty("birthDate");
ok(birthDate === null, "birthDate should be null");
var q = EntityQuery.from("Employees").where("birthDate", "==", null);
stop();
em.executeQuery(q).then(function(data) {
var empsWithNullBirthDates = data.results;
ok(empsWithNullBirthDates.length > 0, "should be at least 1 employee with a null birthdate");
empsWithNullBirthDates.forEach(function(emp) {
var birthDate = emp.getProperty("birthDate");
ok(birthDate === null, "queried birthDate should be null");
});
}).fail(testFns.handleFail).fin(start);
Related
I'm working on a project that keeps track of the hours employees have worked between different crews. The way the app is supposed to work is that an employee submits a Daysheet form that has their clock-in and clock-out time and the id of their Field Manager(FMId). Each Field Manager has a column in the database that should keep track of the DaysheetIds of forms that are submitted with their FMId. The problem is that this column is only saving the first DaysheetId that gets submitted. Once there's a value in that column, the Field Manager doesn't update to add any other DaysheetIds.
To keep my post concise, I'm trying to post only the relevant code, but let me know if I'm missing something that would be important.
Here's the action in my controller where the Field Manager should be updated. I added the var FM just before the return to see what was happening to the Field Manager with the debugger.
public IActionResult Submit(EmployeeDaySheetViewModel employeeDaySheetViewModel)
{
if (ModelState.IsValid)
{
var newDaySheet = new EmployeeDaySheet
{
Id = employeeDaySheetViewModel.DaysheetId,
EmployeeId = employeeDaySheetViewModel.EmployeeId,
EmployeeName = employeeDaySheetViewModel.EmployeeName,
FMId = employeeDaySheetViewModel.FMId,
Date = employeeDaySheetViewModel.Date,
ClockIn = employeeDaySheetViewModel.ClockIn,
ClockOut = employeeDaySheetViewModel.ClockOut,
};
var success = _employeeDaySheetRepository.AddDaySheet(newDaySheet);
if (success)
{
_applicationUserRepository
.AddCrewDaySheetToFieldManager(employeeDaySheetViewModel.FMId,
employeeDaySheetViewModel.DaysheetId);
TempData["UserMessage"] = "Successfully submitted daysheet for " + DateTime.Now.Day.ToString();
}
else
{
TempData["ErrorMessage"] = "Unable to submit daysheet. Please try again in a few minutes.";
}
}
var FM = _applicationUserRepository.GetFieldManagerById(employeeDaySheetViewModel.FMId);
return Redirect("/home/");
Here is the AddCrewDaySheetToFieldManager method in my user repository that's being called in the controller action:
public bool AddCrewDaySheetToFieldManager(string FMId, string DaySheetId)
{
var fieldManager = _applicationDbContext.ApplicationUsers
.FirstOrDefault(u => u.Id == FMId);
if (fieldManager.CrewDaySheetIds != null)
{
var oldCrewIds = fieldManager.CrewDaySheetIds;
// If the user deletes all their crewIds, the database leaves an empty string in their crewIds column.
// Replacing user.crewIds that has "" at index 0 is the same as starting a new list.
if (oldCrewIds[0] == "")
{
var newCrewIds = new List<string> { DaySheetId };
fieldManager.CrewDaySheetIds = newCrewIds;
}
else
{
fieldManager.CrewDaySheetIds.Add(DaySheetId);
}
}
else { fieldManager.CrewDaySheetIds = new List<string> { DaySheetId }; }
_applicationDbContext.SaveChanges();
return true;
This is what my ApplicationUser looks like:
public class ApplicationUser : IdentityUser
{
public string Tier { get; set; }
public bool IsFieldManager { get; set; }
public double HourRate { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<string> PayrollObjectIds { get; set; }
public List<string> CrewDaySheetIds { get; set; }
}
In order to convert the lists attributes of this object to strings, I've got this in my DbContext
protected override void OnModelCreating(ModelBuilder builder)
{
var splitStringConverter = new ValueConverter<List<string>, string>(v => string.Join(";", v), v => v.Split(new[] { ';' }).ToList());
builder.Entity<ApplicationUser>().Property(nameof(ApplicationUser.PayrollObjectIds)).HasConversion(splitStringConverter);
builder.Entity<ApplicationUser>().Property(nameof(ApplicationUser.CrewDaySheetIds)).HasConversion(splitStringConverter);
base.OnModelCreating(builder);
//I also seed some data here//
}
What I can't figure out is the point at which the data is not getting saved. When I run the debugger, I can see the fieldManager is getting updated. In the AddCrewDaySheetToFieldManager method, fieldManager.CrewDaySheetIds has the DaysheetId. Then, back in the action, just before return Redirect('/home/')the FM.CrewDaySheetIds still has the DaysheetId. However, when I look at the database or try to access the CrewDaysheetIds on the FM user, only the first DaysheetId is there.
I suspect that there's something going wrong with the splitStringConversion, but I've used the same code in another project and not had this issue, so I'm stuck for what to do.
I didn't understand your question correctly but if you want update or insert some data in your database you can use _applicationDbContext.ApplicationUsers.add(fieldManager); for insert, and _applicationDbContext.Entry(fieldManager).State = EntityState.Modeified; for update. but you didn't use any of them before _applicationDbContext.SaveChanges().
I think you have to write this:
public bool AddCrewDaySheetToFieldManager(string FMId, string DaySheetId)
{
var fieldManager = _applicationDbContext.ApplicationUsers
.FirstOrDefault(u => u.Id == FMId);
if (fieldManager.CrewDaySheetIds != null)
{
var oldCrewIds = fieldManager.CrewDaySheetIds;
// If the user deletes all their crewIds, the database leaves an empty string in their crewIds column.
// Replacing user.crewIds that has "" at index 0 is the same as starting a new list.
if (oldCrewIds[0] == "")
{
var newCrewIds = new List<string> { DaySheetId };
fieldManager.CrewDaySheetIds = newCrewIds;
}
else
{
fieldManager.CrewDaySheetIds.Add(DaySheetId);
}
}
else { fieldManager.CrewDaySheetIds = new List<string> { DaySheetId }; }
_applicationDbContext.entry(fieldManager).State = EntityState.Modified;
_applicationDbContext.SaveChanges();
return true;
I am writing a custom attribute to validate that a first and last name does not exceed a certain amount of characters, but the error message is not displaying like it does for out-of-the-box annotations.
Here is my implementation.
public class User
{
[Required(ErrorMessage = "Last Name is required.")]
[RegularExpression(#"^[a-zA-Z'\s]{1,50}$", ErrorMessage = "Please enter a valid last name.")]
[FullNameMaxLength("FirstName")]
public string LastName { get; set; }
}
public class FullNameMaxLengthAttribute : ValidationAttribute
{
private string _firstName;
public FullNameMaxLengthAttribute(string firstName)
{
_firstName = firstName;
}
protected override ValidationResult IsValid(object lastName, ValidationContext validationContext)
{
clsUserRegistration userRegistrationContext = (clsUserRegistration)validationContext.ObjectInstance;
if (lastName != null)
{
string strValue = lastName.ToString();
PropertyInfo propInfo = validationContext.ObjectType.GetProperty(_firstName);
if (propInfo == null)
return new ValidationResult(String.Format("Property {0} is undefined.", _firstName));
var fieldValue = propInfo.GetValue(validationContext.ObjectInstance, null).ToString();
if (strValue.Length + fieldValue.Length > 53)
{
return new ValidationResult("First and last names are too long!");
}
return ValidationResult.Success;
}
return null;
}
}
In my view, I have a ValidationMessageFor, and it works fine with non-custom attributes. When I step through my model, it returns the ValidationMessage, but I cannot see that error message. Any thoughts?
The above is just the "back-end" validation. This for example does still work when user's browser has JavaScript turned off - the page will post back regardless of errors but then show the form again with validation messages on it.
For "front-end" validation, you need something along these lines:
public class FullNameMaxLengthAttribute : ValidationAttribute, IClientValidatable
{
// Your Properties and IsValid method here
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = String.IsNullOrEmpty(ErrorMessage) ? FormatErrorMessage(metadata.DisplayName) : ErrorMessage,
ValidationType = "fullnamemaxlength"
};
rule.ValidationParameters["firstname"] = FirstName;
rule.ValidationParameters["maxlength"] = 53;
yield return rule;
}
}
And then in JavaScript that is added to the page:
if (jQuery.validator) {
jQuery.validator.addMethod("fullnamemaxlength", function(value, element, param) {
var name = param.firstname;
var max = param.maxlength;
return name.length > max;
});
jQuery.validator.unobtrusive.adapters.add("fullnamemaxlength", ["firstname", "maxlength"], function (options) {
options.rules.fullnamemaxlength = {};
options.rules.fullnamemaxlength.firstname = options.params.firstname;
options.rules.fullnamemaxlength.maxlength = options.params.maximum;
options.messages.fullnamemaxlength = options.message;
}
);
}
Note this sits OUTSIDE of document.ready() { };
Something similar here: client-side validation in custom validation attribute - asp.net mvc 4
I have hit a problem building a uCommerce site based on top of the demo razor store available http://thesitedoctor.co.uk/portfolio/avenue-clothingcom/
The demo uses servicestack and the ucommerceapi for its basket functions.
I am trying to add a dynamic property to the basket (on an order line) at the point where the user clicks buy. I traced through the productpage.js file and amended the code to add a new property ('message'):
function (data) {
var variant = data.Variant;
$.uCommerce.addToBasket(
{
sku: variant.Sku,
variantSku: variant.VariantSku,
quantity: qty,
message: $('#personalisedMessage').val()
},
function () {
updateCartTotals(addToCartButton);
}
);
});
using firebug, i checked the data that is being posted
addToExistingLine: true
message: "this is a message"
quantity:"1"
sku: "Product (options: none)"
variantSku:""
Posting this does not cause an error, but I cannot tell if it has worked either - I cannot find it in the database, assuming that it would be stored in OrderProperty table. In this scenario, I am 'buying' a product with no variations.
Any help is greatly appreciated with this.
Out of the box you can't add order/line item properties via the API like that. The API payload that you've added to is specified although valid JSON won't get interpreted/used by the API.
Instead what you'll need to do is add your own method to the API. To do this you'll need to implement a service from IUCommerceApiService and then you can do what you need. I've created an example (untested) below and will get it added to the demo store as I think it's a useful bit of functionality to have.
public class AddOrderLineProperty
{
public int? OrderLineId { get; set; }
public string Sku { get; set; }
public string VariantSku { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
public class AddOrderLinePropertyResponse : IHasResponseStatus
{
public AddOrderLinePropertyResponse() { }
public AddOrderLinePropertyResponse(UCommerce.EntitiesV2.OrderLine line)
{
if (line == null)
{
UpdatedLine = new LineItem();
return;
}
var currency = SiteContext.Current.CatalogContext.CurrentCatalog.PriceGroup.Currency;
var lineTotal = new Money(line.Total.Value, currency);
UpdatedLine = new LineItem()
{
OrderLineId = line.OrderLineId,
Quantity = line.Quantity,
Sku = line.Sku,
VariantSku = line.VariantSku,
Price = line.Price,
ProductName = line.ProductName,
Total = line.Total,
FormattedTotal = lineTotal.ToString(),
UnitDiscount = line.UnitDiscount,
VAT = line.VAT,
VATRate = line.VATRate
};
}
public ResponseStatus ResponseStatus { get; set; }
public LineItem UpdatedLine { get; set; }
}
public class AddOrderLinePropertyService : ServiceBase<AddOrderLineProperty>, IUCommerceApiService
{
protected override object Run(AddOrderLineProperty request)
{
var orderLineId = request.OrderLineId;
var sku = request.Sku;
var variantSku = request.VariantSku;
var orderLine = findOrderLine(orderLineId, sku, variantSku);
addPropertyToOrderLine(orderLine, request.Key, request.Value);
TransactionLibrary.ExecuteBasketPipeline();
var newLine = findOrderLine(orderLineId, sku, variantSku);
return new AddOrderLinePropertyResponse(newLine);
}
private void addPropertyToOrderLine(OrderLine orderLine, string key, string value)
{
if (orderLine == null)
return;
orderLine[key] = value;
orderLine.Save();
}
private static OrderLine findOrderLine(int? orderLineId, string sku, string variantSku)
{
return orderLineId.HasValue
? getOrderLineByOrderLineId(orderLineId)
: getOrderLineBySku(sku, variantSku);
}
private static OrderLine getOrderLineBySku(string sku, string variantSku)
{
return String.IsNullOrWhiteSpace(variantSku)
? getOrderLines().FirstOrDefault(l => (l.Sku == sku))
: getOrderLines().FirstOrDefault(l => (l.Sku == sku && l.VariantSku == variantSku));
}
private static OrderLine getOrderLineByOrderLineId(int? orderLineId)
{
return getOrderLines().FirstOrDefault(l => l.OrderLineId == orderLineId);
}
private static ICollection<OrderLine> getOrderLines()
{
return TransactionLibrary.GetBasket().PurchaseOrder.OrderLines;
}
}
You'll need to add the new method to uCommerce.jQuery.js as well something like this:
addOrderLineProperty: function (options, onSuccess, onError) {
var defaults = {
orderLineId: 0
};
var extendedOptions = $.extend(defaults, options);
callServiceStack({ AddOrderLineProperty: extendedOptions }, onSuccess, onError);
}
Let me know if you have any issues using it.
Tim
Does SignlaR automatically map json object sent from to client to c# object? if so, what could i be doing wrong here?
C# Object
public class ChatHub :Hub
{
public void broadcastMessage(CommentModel model)
{
string test = model.Comment;
// Clients.All.writeMessage(jsonString);
}
public class CommentModel
{
[Required]
public string Name { get; set; }
[Required]
public string Comment { get; set; }
[Required]
public string EmailAddress { get; set; }
}
}
JavaScript
$(document).ready(function () {
var chat = $.connection.chatHub;
chat.client.writeMessage = function (t) {
var name = t.Name;
var email = t.Email;
var id = t.id;
var text = name + " " + email + " " + id + " ";
$("#test").append(text);
}
$("form").submit(function (e) {
var jsonObject = JSON.stringify($(this).serializeObject());
chat.server.broadcastMessage(jsonObject);
e.preventDefault();
});
$.connection.hub.start();
});
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
You seem to be sending a Json string to the app server whereas the server is expecting an object.
Change:
var jsonObject = JSON.stringify($(this).serializeObject());
To:
var jsonObject = $(this).serializeObject();
Model (exposed mapping table for many-to-many relation):
public class TeamUsers
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long TeamUsersId { get; set; }
public Team Team { get; set; }
public User User { get; set; }
public bool Temporary { get; set; }
}
Two general methods for remote and local query:
getAllOfTypeSelectRemote = function (manager, success, failed, resource, orderBy, predicate, select) {
var query = EntityQuery
.from(resource)
.where(predicate)
.using(manager)
.select(select) //<------
.orderBy(orderBy);
return query.execute()
.then(success)
.fail(failed);
},
getAllOfTypeSelectLocally = function (manager, resource, orderBy, predicate, select) {
var res = null;
try {
res = EntityQuery
.from(resource)
.where(predicate)
.select(select) //<-----
.orderBy(orderBy)
.using(manager)
.executeLocally();
} catch (e) {
logger.error('resource: ' + resource + '<br>select: ' + select + '<br>orderBy:' + orderBy, 'Local query failed!');
}
return res;
};
called this way:
var selectStr = 'User'; //<------
var p1 = new breeze.Predicate("Team.TeamName", Qop.Equals, team);
var p2 = breeze.Predicate("User.UserName", Qop.NotEquals, username);
var predicate = p1.and(p2);
//var res = dsUtils.getAllOfTypeSelectRemote(manager, success, queryFailed, 'TeamUsers', 'User.Nachname, User.Vorname', p1, select);
var res = dsUtils.getAllOfTypeSelectLocally(manager, 'TeamUsers', 'User.Nachname, User.Vorname', p1, selectStr);
The remote query returns an array of User-Entities which is correct i believe. Modifying 'selectStr' to an invalid type like 'bla' throws an exception as expected.
result:
data.results = [{"User": {...}}, {"User": {...}}]
The local query just ignores the select and returns an array of TeamUser
result:
res=[{ Here are the fields of TeamUsers }, { Here are the fields of TeamUsers }]
Modifying 'selectStr' to an invalid type like 'bla' throws NO exception.
Why is select omitted, is this supposed to be like that?
As of v 0.74.3, this should be fixed; i.e. select clauses will now be correctly interpreted by local queries as well as remote queries. Hope this helps.