org.openxava.util.XavaException:cannot add new element in collection because an entity property is readonly..but i did not put a property readonly inside my entity class.
IT WAS A TYPE MISTMATCH PROBLEM. WHEN I LOOKED CLOSELY TO THE CODE AND PUT THE CORRECT TYPE FOR THE ENTITY PROPERTY...IT WORKS!!! IM ABLE TO ADD ELEMENT TO THE COLLECTION.
Related
In Hybris 6.7.0, using I have a component that contains a localized list of another component.
I have been able to implement this using a n-m localized relationtype to implement a localized list that contains the component.
It works perfectly in CMS cockpit. However, in smartedit, it causes a ClassCastException.
The default converter seems to fail to recognize the collection type and therefore trying to cast the collection to a item model which causes the error.
Is there anyway to implement a localized collection that won't causes the exception in SmartEdit?
I have tried using a map and collection pair for the localization instead of a localized relation but the same problem occurred.
2019-3-7 - Updates: After a series of trial and error, I have realized that the LocalizedCollection will never be called because all localized attribute in Hybris is stored with an item type of MapType, which does not trigger the localizedCollection getter as it check if the localized attributs is of type CollectionType.
It seems to be a bug on SAP side. I am currently trying to come up with a temporary fix for the problem.
I am unable to create new user with below code :
final EmployeeModel empl=new EmployeeModel();
empl.setUid("test");
modelService.save(empl);
Also, don't do this: new EmployeeModel();
You should always use the ModelService to create new instance of ItemModel:
modelService.create(EmployeeModel.class)
Using modelService, it will go through any Interceptor that's define for this type and then, could initialize default value for your item. When your using the new operator. It won't set the default value, and can result in the kind of error you are facing.
This code works in an hybris out of the box.
You have probably customized the employee type and added mandatory fields.
Check the error detail, you will probably find out the field you missed.
I am having trouble posting a form that contains a Dictionary that contains an int as a key and a list of objects as a value.
Originally this was just a List of Objects and that worked fine and the type was:
List<MyObject> Fields
the working markup was
Fields_{0}__Property1
where {0} is the index of the object. To get it to post back the List of Objects I rendered the object with hidden fields like this:
#Html.HiddenFor(m => m.Property1, new { Name = string.Format("Fields[{0}].Property1", Model.Index), #id = string.Format("Fields_{0}__Property1", Model.Index) })
This worked well. Now however, we have a dictionary instead of a list and the list is inside the dictionary.
Now the type is:
Dictionary<int, List<MyObject>>.
I tested the format expected when we render the dictionary using Html.HiddenFor and so I've added hidden fields with the required format which now is:
#Html.HiddenFor(m => m.Property1, new { Name = string.Format("Fields[{0}][{1}].Property1", Model.Index, Model.Position), #id = string.Format("Fields_{0}__{1}__Property1", Model.Index, Model.Position) })
now the field id is
Fields_{0}__{1}__Property1
where {0} is the key of the dictionary and {1} is the index of the object in the list.
However on postback I now get
[InvalidCastException: Specified cast is not valid.]
System.Web.Mvc.CollectionHelpers.ReplaceDictionaryImpl(IDictionary`2 dictionary, IEnumerable`1 newContents) +131
I am guessing MVC is smart enough to render the fields of this complex object on the view but not smart enough to collect them back into the viewmodel when we post back.
I found this other guy who had a similar problem here and he solved it by not using a dictionary but instead creating a complex object. I'm wondering, however, if there's a quicker way that won't require me to rewrite the entire system.
Any ideas?
Update
I solved it by taking the source code of DefaultModelBinder and adjusting it.
I found the source here. I didn't create my own Binder because I want all the advanced functionality and validation rules to apply to all other elements.
Once I got the DefaultModelBinder compiling and working I found the part where the dictionary was failing to cast the complex items and wrote a custom Dictionary update method that solved the problem
You can always create a custom Model Binder to bind objects from request values exactly as you want. Simply create a class that implements the System.Web.Mvc.IModelBinder interface and implement the BindModel() method.
When handling the Selected event of a EntityDataSource, the Results property of the EntityDataSourceSelectedEventArgs returns an ObjectView collection of my entities. I'm not sure what this class is. This link to the Namepace doesn't mention the class.
Is there any documentation on this System.Data.Objects.ObjectView collection class? How would I convert an ObjectView<T> to a List<T>? Perhaps it's as simple as enumerating the collection and adding the items to a new List<T>, but some information about the class would be useful.
The ObjectView class is internal and therefore you cannot find it on MSDN. The important bit of information is that it implements IBindingListInterface
I have a CollectionViewSource that gets bound to an ObservableCollection in a ViewModel, for this we'll call it
ObservableCollection<ItemCollection> Items;
Now, I have another collection which holds a Collection of Collections that gets updated and returned from a service (the service is updating the actual object, the container is static).
ObservableCollection<ItemCollectionContainer> Container;
Each "Container" has a property "Items" which has a list.
What I want to display is the complete List of ALL Container.Items put in 1 collection, and if possible, if an items gets added to Container.Items, it gets updated.
Any Ideas?
Thanks!
All I can say is I must fail in explaining what I was looking for of it was so simple, people wanted me to figure it out myself :)
Answer is
viewModel.Items = new ObservableCollection<Item>(Container.SelectMany(x => x.Items));