I have the following validation not working and cannot resolve why that is the case?
[Display(Name = "Bus")]
[RequiredIf("SelectedWidgetText == 'Referral'", ErrorMessage = "Please select a Vehicle.")]
public int DepotId{ get; set; }
public string SelectedWidgetText { get; set; }
I have also tried the following:
[Display(Name = "Bus")]
[AssertThat("SelectedWidgetText == 'Referral'", ErrorMessage = "Please select a Vehicle.")]
public int DepotId{ get; set; }
on the server side, when the modelstate.isvalid method is hit, it returns false and the error message thrown is The DepotId field is required.
just by changing the DepotId to Nullable like the following, the validation started working as expected:
public int? DepotId{get;set;}
After reading the documentation, looks like the value types have to be nullable before the data annotation will work on them.
Related
I'm using the following code to query an excel file in LinqToExcel:
var excelFile = new LinqToExcel.ExcelQueryFactory(#"\"+txtFileName.Text.Replace(#"\\",#"\"));
var properties = from p in excelFile.Worksheet<Property>()
where AssessmentID != null
select p;
foreach (var autoP in properties)
doSomething();
When I look at the runtime debugger, I see an "InvalidCastException" while looking at the "Results View" of the properties variable. So, I'm assuming that there's something funky going on with my class definition. I'm also assuming that I don't need to map all members of the class to the excel file, but rather only the ones I see fit.
So, Here's the class definition as well:
public class Property
{
[DataMember]
public int? Year { get; set; }
[DataMember]
public string ChangeReason { get; set; }
[DataMember]
public string AssessmentID { get; set; }
[DataMember]
public string CallBackNotes { get; set; }
[DataMember]
public string InspectionNotes { get; set; }
[DataMember]
public string Notes { get; set; }
[DataMember]
public bool Authorization { get; set; }
[DataMember]
public string ChargeStatus { get; set; }
[DataMember]
public string LegalLandDesc { get; set; }
[DataMember]
public string Address { get; set; }
}
Here's a link to the source code for linqToExcel on GitHub:LinqToExcel
The generics are more complicated than I've seen, and are something that I (apparently) need to brush up on.
Is there something glaring that I'm missing that would cause this issue? Any ideas as to where to look or what to do to resolve these errors?
The where clause of the query is what was causing the error. It was referencing the null keyword which was not applicable in the context.
I changed it to:
where !String.IsNullOrEmpty(p.AssessmentID)
This solved my issue.
Note to self: When inheriting code from others, check the basics first!
I have a model...
public class PatientACOModel
{
public int EncounterId { get; set; }
public int PatientId { get; set; }
public int EMPIID { get; set; }
public int PopulationPatientID { get; set; }
public string EditAddOrEditCurrent { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
//[UIHint("_PhoneNumFormatter")]
public string Phone { get; set; }
}
I want to specifically format my phone numbers. I just want to put a UIHint over my phone number and possibly other phone numbers. I don't want all strings to be formatted.
I am trying things like this...
#Model String
#if (Model != null) {
String.Format("{0: (###) ###-####}", double.Parse(Model.ModelMetadata.Get));
}
That would be the display template referenced in my UIHint that is commented out.
When I do that, none of my strings show up. What am I doing wrong?
In my display template how do I get the string to parse and then Format?
First I would check if the _PhoneNumFormatter is defined in one of the following locations:
~/Views/Shared/DisplayTemplates/_PhoneNumFormatter.cshtml
~/Views/<Controller>/DisplayTemplates/_PhoneNumFormatter.cshtml
Next I think you wanted to format the string value directly from the Model property:
#model String
#if (!String.IsNullOrWhiteSpace(Model))
{
#String.Format("{0: (###) ###-####}", Convert.ToInt64(Model))
}
This should work fine with the UIHint attribute, I've just checked in a sample application.
Code assumes that numbers in your region don't start with a leading 0 and that the Model property will contain strings containing only numeric characters, if not you should improve the Convert.ToInt64 part.
Hope it helps.
My project is the throwing the above error because the Ship Date in my table is null. I am using Code First, and the DateTime field is declared as nullable both in the generic set and the SQL table. I am populating a gridview with the result set. Ideally, I would like to have the gridview display "not yet shipped" when the ShipDate field is null, but at this point, I would be happy just to be able to display the record. Any help is greatly appreciated. Here is the code-behind and the context class that I am using:
public class Order
{
[Key]
public int OrderID { get; set; }
public int CustomerID { get; set; }
public string ShipAddress { get; set; }
public string ShipCity { get; set; }
public string ShipState { get; set; }
public string ShipZip { get; set; }
[ScaffoldColumn(false)]
public string PaymentTransactionID { get; set; }
[ScaffoldColumn(false)]
public System.DateTime OrderDate { get; set; }
public System.DateTime? ShipDate { get; set; }
public virtual Customers Customers { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
}
The code behind is:
public List<OrderDetail> FetchByOrderID()
{
int myOrderID = CusOrderId;
ProductContext db = new ProductContext();
return db.OrderDetails.Where(
c => c.OrderID == myOrderID).ToList();
}
I was getting the above error when trying to get the max(date_column) through LINQ. The column was defined as NOT NULL and when there are no rows returned in the query its trying to assign null to the DateTime property that's representing the date column. One solution is to change the date column to being NULLable which will create the entities property as type DateTime?
I use VB.Net, but had a similar problem. I do not know if you fixed your problem.
But I declared the variable as Nullable(Of Date) and that did the trick to me. I am told that adding the "?" does the similar thing, but unfortunately did not work out for me, or I did not do it correctly. :o)
private DateTime? _settlementDate {get;set;}
I was having a similar problem, same error.
Turns out I failed to setup my ConnectionString in the configuration.
I am attempting to update an entry in Azure Table Storage. The function is:
public void SaveBug(DaBug bug)
{
bug.PartitionKey = "bugs";
bug.Timestamp = DateTime.UtcNow;
if (bug.RowKey == null || bug.RowKey == string.Empty)
{
bug.RowKey = Guid.NewGuid().ToString();
_context.AddObject(c_TableName, bug);
}
else
{
_context.AttachTo(c_TableName, bug);
_context.UpdateObject(bug);
}
_context.SaveChanges();
}
If it is a new entry (the "bug.RowKey == null" path), then it works fine. If it is an update to an existing entity, then the "AttachTo", and the "UpdateObject" calls work, but when it gets to "SaveChanges", it throws the "One of the request inputs not valid" exception.
The class that is being stored is:
[DataContract]
[DataServiceKey("RowKey")]
public class DaBug
{
[DataMember]
public bool IsOpen { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string SubmittedBy { get; set; }
[DataMember]
public DateTime SubmittedDate { get; set; }
[DataMember]
public string RowKey { get; set; }
public DateTime Timestamp { get; set; }
public string PartitionKey { get; set; }
}
Does anyone know what the problem is?
Thanks for any help.
In case anyone is looking for the answer:
http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/0c9d476e-7970-422a-8b34-e987e41734df
Working through the table context, I had to change the call from:
_context.AttachTo(c_TableName, bug);
to:
_context.AttachTo(c_TableName, bug, "*");
You can also get this error if you mistakenly set the RowKey to a value you've already used (not that you'd get this problem with code in the question). I tried to push 50+ entities in one go and accidentally had the RowKey set to the same value for two of the entities.
I am trying to add a boolean column in SubSonic 3.0.0.3 and without this column the code works fine but as soon as I had a bool variable into my model this fails with the following error:
The name "False" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Anyonw know if this should be supported and if it is what I may be doing wrong:
Data Object Class:
public class Desk
{
[SubSonicPrimaryKey]
public int DeskId { get; set; }
public string DeskName { get; set; }
public string SAPCode { get; set; }
public int LocationId { get; set; }
public bool Active { get; set; }
}
Use of Class:
var d = new Desk();
d.DeskName = "Test";
d.SAPCode = "12345";
d.LocationId = 2;
d.Active = true;
var repository = new SimpleRepository("SubSonicTesting", SimpleRepositoryOptions.RunMigrations);
repository.Add(d);
I have faced the exact same issue (version 3.0.0.3) when I added a bool property named "IsAccountOwner". The problem seems to be with migrations because when I deleted the table, SubSonic re-created it correctly with the added column.
I'm using SQL Server 2008 Express, in case that matters. The error is related to a malformed query perhaps?