How to pass checkbox checked values as parameter to controller action and return Json data which bind to Listbox in view in C# MVC? - checkbox

checkbox checked values as parameter to controller action and return Json data which bind to Listbox in view

Related

Two ListViews with different filters on the same data set

I have a ViewModel with some ObservableCollection _questions, which is loaded from DB when VM instance created. Also this list is used to collect data to save back to DB.
This Vm is used for a View1 and it displays the list in ListView with filtering by a property using CollectionViewSource.GetDefaultView(_questions).Filter = ...
Now I need to create View2 which will display same list but without filtering.
I can't bind it to the same ObservableCollection _questions because it has filter defined on CollectionViewSource, but I need to use it to keep SaveToDb code same.
Is it possible to have different filtering on the same data source for two different ListViews?
I have never enjoyed using CollectionViewSource. I would instead filter using a new property in my ViewModel that filters using Linq:
public IEnumerable<MyType> FilteredItems()
{
get
{
return MyCollection.Where(x => x.MyProperty == SomeValue).ToArray;
}
}
I would then bind ItemsSource to this property and use INotifyPropertyChanged event to notify UI of changes to the collection.
Its hard to tell if this fits your scenario well as there is not much information provided on what you need to achieve.

Binding CommandParameter to code behind Property

I have a WPF View where I create controls dynamically depending on the object type in a List that the View is bound to in the ViewModel.
I have a Button on in the View that I have bound to a RelayCommand in the ViewModel but I also want to pass a CommandParameter to the Command.
The dynamically create controls are of types ListBox, ComboBox, TreeView, RadionButton and CheckBox. So when I press the Button I want to get all selected/checked items in the controls and pass this a List with the CommandParamter.
I have figured out how to search for all selected/checked items and get the object of type Code(class name) and put them in a List if I use the Click event on the Button. I want to use the RelayCommand instead of using Click Event.
I have managed to bind the CommandParameter to the a Property that calls GetAllSelectedCheckedCodes() but it is only bound when the View is created.
Is it possible to have a property in the code behind that calls the function GetAllSelectedCheckedCodes() that is bound first when the Button Command is triggered.
Or is it possible to have multiple controls add/remove items in a List in the ViewModel when they are selected/unselected or checked/unchecked?
Are you able to add a property to your class like IsSelected, or IsActive, and bind that to the the IsSelected or IsChecked property of the control? Then, when you want to act on the selected items, you could just grab all the items in the collection where IsActive == true.

Selecteditem event in MVVM silverlight

i have a datagrid bound to a property. In this grid i have columns which consists of cells which are like hyperlink i mean when user clicks on the cell value based on these values another gird will get populated. i want to know how to get the cell value and pass it to some method so that other grid will get populated.
The best way to do this is in your viewmodel.
You should bind the SelectedItem of your datagrid to a new property in your ViewModel. In the set method of this new Property, call a new method to populate a new ObservableCollection/List/whatever...
Finally, bind your "other grid" ItemsSource to this new observable collection from your ViewModel.
Edit:
If you need to load one thing or another depending on the column you are going to use the code behind, take a look at this:
Silverlight DataGrid how to get cell value from a selected item?

Multiple Selection Combobox

I have a scenario where there is a combobox with checkboxes, so multiple selection is possible. After selecting the combobox items, I need to execute an update query. I know to write the query for single item, but how to do the query for multiple selections from combobox.
Regards,
Raghu
The combobox shows items based on a list of view models, lets name them CheckableViewModel in the view model that is the data context of te view:
class ViewModel
{
//...
List<CheckableItemViewModel> Items;
//...
}
<ComboBox ItemsSource="{Binding Items}"
<!-- more properties --> />
As far as querying data is a task done in the data logic this task has also to be triggered there. This means that your CheckableItemViewModel has to signal a change of it's checked state, e.g. via an event (at least PropertyChanged on property Checked). The view model that contains the list of CheckableItemViewModels, which is the view model the UI binds to, has to listen for this event. And this view model has to contain the logic to call the appropriate query containing the appropriate data.

How do you get the visual element inside a ItemsControl

I have a list of States in an ItemsControl, the DataTemplate is a CheckBox. I would like to add a function to select(check) all states. However I need to loop through the CheckBoxs rather then the data items since the checked state is stored in a separate data structure then the list of states the ItemsControl's ItemSource is bound to.
Have a property in your DataObject called IsChecked and bind that to the Checkbox in DataTemplate(Default is TwoWay)
In the data template I subscribed to the checkbox's onload event. And in the event handler I add the checkbox reference to a generic list of checkbox.

Resources