ListBox Save

Hi

I have a question on the listbox:

Does the Listbox save the ticked item(s) to the database as and when they are ticked? Or do I have to somehow handle the change event and do it myself? Its bound to my datasource.

I have a bit field in the db (IsSelected) that I want to toggle as the tick box is toggled.

Can you clarify the difference between Value and ValueProperty ?

As a multi select listbox I have set ValueProperty to my data (same as data property) and ValueProperty to the IsSelected bit field.Have tried changing these but no luck and the tickbox refuses to toggle

I'm going around in circles trying to get it to work despite looking at docs.

I can see the items in the list bound from the data but unable to get them to save.

Thanks
Tim

Does the Listbox save the ticked item(s) to the database as and when they are ticked?

No. No Radzen Component saves values to the database. They trigger events and the developer is responsible for persisting the changes to the database. Radzen itself generates the required code for that - event handlers for Click events and Invoke data source method actions.

Can you clarify the difference between Value and ValueProperty ?

The Value property represents the selection / current value of a component. ValueProperty is used to tell the component what property of the model to store inside Value. If not set the component will store the whole data item. The Value property is usually in sync with a page property in Radzen. This allows the developer to use the said page property to persist changes in a database.

I can see the items in the list bound from the data but unable to get them to save.

They will not save automatically - you have to invoke the necessary data source methods that Radzen has generated in order to update the database.

Thanks for the info.

I can put the listbox in a form, getvalues and submit the form and pick up the items in the submit event.
I can iterate over the items and can call the update call (same call that's made when updating an edit record) but it fails on the context.SaveChanges() with a "New transaction is not allowed because there are other threads running in the session"

Do you have a sample that I could build on?

Many thanks
Tim

public async Task<Models.Pda.ComparableResult> UpdateComparableResult(Guid? comparableResultId, Models.Pda.ComparableResult comparableResult)
{
    OnComparableResultUpdated(comparableResult);

    var item = context.ComparableResults
                      .Where(i => i.ComparableResultID == comparableResultId)
                      .First();
    var entry = context.Entry(item);
    entry.CurrentValues.SetValues(comparableResult);
    entry.State = EntityState.Modified;
    context.SaveChanges(); // fails here

    return comparableResult;
}

Unfortunately we don't have a sample and I am not sure what is causing this EF exception. I found this online. The suggestion is "Use AddRange, or move SaveChanges out of the loop.". Moving SaveChanges out of the loop sounds reasonable. You will have to use custom code for that though. Here is an example which shows how to use the database context in a custom method.

Hi

Thanks for the reply.
I chased down the ef exception. You have to turn the IEnumerable into a List then iterate over the list calling the update for each item.

Other issues:

  1. If the listbox is multiselect, and you use a form, the form submit should post back an IEnumerable of type T but it only posts back a single object T,

  2. When setting the Value property, I would assume it would accept a List of my objects that need to be selected on init. The ValueProperty would tell it what property to use, ie the ID or key of the list. That doesn't work, rather I have to pass an array of ints containing the PK (assuming my pk are ints). that then shows the selected items in the list box correctly and updates this list.

In the end I got it to work, I removed the form and handled the listbox clicks on the change event, I then had to add a @bind-Value and remove the Value entry to get it to bind so when I handled the change event I could update the actual objects from the list of ints that get passed in the change event and pass each object to the update (making sure the IEnumerable was converted to a list first to stop ef erroring)

Thanks
Tim