MVC Model Error on TryUpdateModel() - asp.net-mvc

I have an error (see below). At first glance it seems obvious to me, however. I've checked everything: the MODEL is ok; metadata class set ok and i've checked my controller and at the time 'TryUpdateMOdel' is called all is well and the object is as i expect it to be. I'm thinking this will be something silly but been stuck all day, anyone recommend anything?
The associated metadata type for type 'Lms.Model.PaymentFrequency' contains the following unknown properties or fields: SiteAgreementId, PaymentTypeId, PaymentCategoryId, ObligationStartDate, TerminationDate, Comments. Please make sure that the names of these members match the names of the properties on the main type.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The associated metadata type for type 'Lms.Model.PaymentFrequency' contains the following unknown properties or fields: SiteAgreementId, PaymentTypeId, PaymentCategoryId, ObligationStartDate, TerminationDate, Comments. Please make sure that the names of these members match the names of the properties on the main type.
Source Error:
Line 120: PaymentFrequency paymentFrequency = this._siteRepository.GetPayment(Convert.ToInt16(collection["PaymentId"])).PaymentFrequency;
Line 121:
Line 122: TryUpdateModel(paymentFrequency);
Line 123:
Line 124: if (!ModelState.IsValid)

It sounds like the metadata class you have attached to your PaymentFrequency model may have properties the model itself doesn't.

Related

Error creating event - The property 'address' does not exist

Just started repeatedly getting this error, with the same request structure the app was successfully using before to create an event using the Microsoft Graph API -
"The property 'address' does not exist on type 'Microsoft.OutlookServices.EmailAddress'. Make sure to only use property names that are defined by the type or mark the type as open type."
The "attendees" property of the failing request looks like the following:
"attendees":[{"emailAddress":{"address":"emailaddress#host.com"},"type":"required"}]
Any help would be greatly appreciated.

How to find the distinct error messages of model state validation failure in asp.net mvc

I want to filter the Error Messages that gets populated as part of data annotation modelstate validation failure. As in if an array of objects comes as a part of class, and the validation fails for more than one object, I do not want the same message to be added again and again. Instead, I want to find the distinct error messages
string ValidationFailure= string.Join(";", actionContext.ModelState.Values.Distinct().Select(x.ErrorMessage));
But not able to get the required output.
It looks like your attempt is close, but you’re using Distinct on something that’s already unique (Values). Instead, try the following variation:
string ValidationFailure = string.Join(";", actionContext.ModelState.Values.Select(x => x.ErrorMessage).Distinct());
This ensures that you get a distinct list of ErrorMessages.

Code=134110 - Validation error missing attribute values on mandatory destination attribute

Context:
app using CoreData
some lightweight migration successfully performed in the past (reached the 4th iteration of the model version)
client wants a new feature
created a 5th model version
added one single lousy new property, a non-optional boolean called new_one, to the TestModel entity
The outcome:
CoreData: error: NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=134110 \"An error occurred during persistent store migration.\"
UserInfo={
entity= TestModel,
attribute=new_one,
reason=Validation error missing attribute values on mandatory destination attribute}";
}
Solution:
I don't completely grasp why this happens (I'm too tired and eager to leave this problem behind), but the "mandatory destination attribute" thing pointed me in the direction of setting the property as an optional. Whether it's the right thing to do or just an ordinary hack...I don't know...but it solved my problem, I can now move on to the next
You've pretty much hit the nail on the head but it sounds like maybe you don't know why. It's because:
The attribute was required
Which means it must have a value when changes are saved
Migration saves changes, but
You didn't provide any value for this attribute.
That leads directly to the error that you received.
You can fix this using any one of the following:
Make the attribute optional, as you did. After migration, no migrated objects have a value, but that's OK.
Keep it non-optional but provide a default value in the model editor. After migration, all migrated objects have the default value.
Set up a non-lightweight migration and provide values when migration occurs. After migration, each migrated object has whatever value you provide during migration.
I think providing a default value is better than superfluous optionality.
Better to use optional only when a value is indeed optional.

About saving data into grails databse

This my project code I want to save my data into database.
def save(){
List<Employee> list = Employee.findAllById(session.getAttribute("empId"))
Milestone milestone = new Milestone()
milestone.setMilestone_Date(params.milestone_Date)
milestone.setMilestone_Name(params.milestone_Name)
milestone.setMilestone_Description(params.milestone_Description)
milestone.save()
EmployeeMilestone employeeMilestone=new EmployeeMilestone()
Employee employee = list.get(0)
employeeMilestone.setEmployee(employee)
employeeMilestone.setMilestone(milestone)
employeeMilestone.save()
[employeeMilestones:employeeMilestone]
}
I am getting this error
Error 500: Internal Server Error URI /ProjectTrackerMain/milestone/save Class java.lang.IndexOutOfBoundsException Message Index: 0, Size: 0
You didn't actually ask a question, so this may be a bit vague!
An IndexOutOfBoundsException happens when you try to access something from a collection in a location where there is no "something". For example, maybe you asked for the tenth element in a list, but there are only two. In your case, you're asking for the zeroth (in plain English, "First") element on this line of code:
Employee employee = list.get(0)
and presumably the list is empty. Your error message says "Size: 0". You can't get the first element from a list that has zero elements in it, so that's an index out of bounds exception.
Why is your list 0? That's a different question. You might try printing out
session.getAttribute("empId")
to see if your employee ID is what you expected. You might also look at the data in your database to see if you actually managed to save an employee! One way or another, you're not getting the data you expected, and then you're trying to use it.
In general, using a debugger to look at your elements, or just using "println" along the way to look at values is helpful in debugging problems like this. That way, you'll find out on line 1 that your list of Employees is not what you expected, instead of several lines later when you try to use it!

entity framework - how do i know which property threw exception during save?

I have a bunch of entities with hundreds of fields in SQL, dozens are non nullable. I was hoping to create the entity, save and see which field its complaining about so that I can easily include this (very quick/easy to do using unit test), but the info i get back from EF does not display which field/property caused the error? Why would this be? I even did try/catch, output exception and no additional info about the problem :-(
System.Data.UpdateException : An error occurred while updating the entries. See the inner exception for details.
----> System.Data.SqlTypes.SqlTypeException : SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
at PaymentDomain.IntegrationTests.EFTestSetup.Test() in EFTestSetup.cs: line 40
--SqlTypeException
at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc, Boolean sync, TaskCompletionSource`1 completion, Int32 startRpc, Int32 startParam)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, ref Task task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, ref Task task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
Cool, how bout something like: CreateDate
Thanks...
You can use DbContext.GetValidationErrors() method to retrieve validation rules break information. Article on MSDN.

Resources