Update List items through Linq, Entity Framework - entity-framework

My list as below:
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "Item 1", Value = "1", Selected = false });
items.Add(new SelectListItem() { Text = "Item 2", Value = "2", Selected = false });
items.Add(new SelectListItem() { Text = "Item 3", Value = "3", Selected = false });
items.Add(new SelectListItem() { Text = "Item 4", Value = "4", Selected = false });
items.Add(new SelectListItem() { Text = "Item 3", Value = "5", Selected = false });
items.Add(new SelectListItem() { Text = "Item 6", Value = "6", Selected = false });
items.Add(new SelectListItem() { Text = "Item 7", Value = "7", Selected = false });
Based on it a listbox is created.
User can select multiple items (though a listbox) and selected items stored as string (3,7 mean Item 3 and Item 7 selected). How I can update the list (before binding) to set selected true for Value 3 and 7?
string selectedItems=3,7
I can do with a for loop and search for item 3 and 7 and update Selected=True
But is there any shortcut or faster way? As I have many instances of such lists in my data entry
Cheers

I did as below:
if (selectedItems != null)
{
var Selected= items
.Where(x => selectedItems.Split(',').Any(s => x.Text.ToString().Equals(s)))
.ToList();
foreach(var item in Selected)
{
item.Selected = true;
}
}
But hopefully some better is there to avoid loop!
Cheers

Related

add label in dropdown item but MUST depends other variable (FLUTTER)

List<Map<String, dynamic>> category = [
{
"name": "One",
"detail": ['11', '12', '13', '14'],
"department": "aaa",
},
{
"name": "two",
"detail": ['21', '22', '23', '24'],
"department": "bbb",
},
{
"name": "three",
"detail": ['31', '32', '33', '34'],
"department": "ccc",
},
{
"name": "four",
"detail": ['41', '42', '43', '44'],
"department": "aaa",
},
{
"name": "five",
"detail": ['41', '42', '43', '44'],
"department": "aaa",
},
];
for (final item in category) {
if (item["department"] == "aaa") {
for (final value in item.values) {
if (value is List) {
for (final listValue in value) {
data.add({'value': listValue, 'bold': false});
}
} else {
data.add({'value': item['department'], 'bold': true});
}
}
}
}
I have used the above (loop) method to doing the dropdown, but the category "name" will repeat many times, as shown in first picture
May I know how to make the list category be like the second picture dropdown, for example, the name will be the label, detail will be item of the label. Lastly, the 'department' is for classify showing which data, let say I want to show the data that department is 'aaa' means that 3 list data will be shown in the dropdown item.
Looking at your data named as "category" which is a list of Maps, I think you can add labels and achieve the required functionality that includes using custom variable in the following way:
const dept = 'aaa';
final data = category.where((element) => element['department'] == dept);
List<DropdownMenuItem<String>>? get_items() {
final List<DropdownMenuItem<String>> _dropdownItems1 = [];
for (final val in data) {
for (final value in val.values) {
if (value is List) {
for (final listValue in value) {
_dropdownItems1.add(
DropdownMenuItem(
child: Text(listValue),
value: listValue,
),
);
}
} else if (value != dept) {
_dropdownItems1.add(DropdownMenuItem(
child:
Text(value, style: const TextStyle(fontWeight: FontWeight.bold)),
value: value,
));
}
}
}
return _dropdownItems1;
}
Now, in the dropdownbutton you can simply call "get_items()" to get the dropdown menu items for creating the dropdown menu.
It can be done as mentioned in the code below.
DropdownButton<String>(
value: selectedItem,
items: get_items(),
onChanged: (value) {
setState(() {
selectedItem = value;
});
}),
The output will be as follows:
Output Dropdown menu

In Flutter, I am trying to make a dependent dropdown with the following json

In Flutter, I am trying to make a dependent dropdown with the following json.
I Want the Dropdown to be in this format
First, Independent Dropdown
dataNames
Second, Dependent Dropdown
indexes of the dataSets' children
Third, Dependent Dropdown
select the children of the above drop-down (...hello 1, hello 2... )
[
{
"dataName": "data1",
"dataSets": [
["hello 1", "hello 2", "hello 3"],
["hi 1", "hi 2", "hi 3", "hi 4"]
]
},
{
"dataName": "data2",
"dataSets": [
["2nd 1", "2nd 2", "2nd 3"],
["let 1", "let 2", "let 3", "let 4"]
]
}
]
Here is the first step: parsing the json into a class model and then creating a list of models. second step build the DropdownMenuItem list to be consumed by the DropDownbutton. Create a _currentResultClass variable the first dropdownbutton (level1) selection. Step 3: filter on level 1 dataName and find the model then populate dropdownmenuitems based on a filter by that dataname.
class ResultClass
{
String?dataName;
//List<List<String>> dataSets= new List.generate(n, (i) => []);
List<List<String>>? dataSets= [];
ResultClass({this.dataName,this.dataSets});
ResultClass.fromJson(Map<String, dynamic> json) {
dataName = json['dataName'];
dataSets = json['dataSets'];
}
Map<String,dynamic> toJson(){
final Map<String,dynamic>data = new Map<String,dynamic>();
data['dataName']=this.dataName;
data['dataSet']=this.dataSets;
return data;
}
}
class TestDropDown extends StatefulWidget {
TestDropDown({Key? key}) : super(key: key);
#override
State<TestDropDown> createState() => _TestDropDownState();
}
class _TestDropDownState extends State<TestDropDown> {
ResultClass ? _currentResultClassLevel1;
String ? _currentStringLevel2;
List<ResultClass> lst=[];
List<DropdownMenuItem<ResultClass>>? itemsLevel1;
var data=[
{
"dataName": "data1",
"dataSets": [
["hello 1", "hello 2", "hello 3"],
["hi 1", "hi 2", "hi 3", "hi 4"]
]
},
{
"dataName": "data2",
"dataSets": [
["2nd 1", "2nd 2", "2nd 3"],
["let 1", "let 2", "let 3", "let 4"]
]
}
];
#override
void initState() {
// TODO: implement initState
super.initState();
for(var i=0; i<data.length; i++)
{
ResultClass model = ResultClass.fromJson(data[i]);
lst.add(model);
if (i==0)
{
_currentResultClassLevel1=model;
}
}
itemsLevel1 = lst
.map((item) => DropdownMenuItem<ResultClass>(
child: Text(item.dataName??""), value: item))
.toList();
}
#override
Widget build(BuildContext context) {
List<ResultClass> models = lst.where((item)=> item.dataName==_currentResultClassLevel1?.dataName).toList();
List<String> strings=[];
models.forEach((model)=>
model.dataSets?[0].forEach((element)=>strings.add(element)));
var itemsLevel2= strings.map((item) => DropdownMenuItem<String>(
child: Text(item), value: item))
.toList();
return Scaffold(appBar: AppBar(title: Text("Dropdown"),),body:
Column(children: [
DropdownButton<ResultClass>(items: itemsLevel1,
value: this._currentResultClassLevel1,
onChanged: (item){
setState(() {
_currentResultClassLevel1=item;
});
},),
DropdownButton<String>(items: itemsLevel2,
value: this._currentStringLevel2,
onChanged: (item){
setState((){
_currentStringLevel2=item;
});
},),
SizedBox(height:50)
],)
);
}
}

How can i return different value from what was in the dropdown in dart?

I am working with dropbox, but what i want to do is retrieve value depending on what the user chooses on the dropbox; for example the users picks "apple" on the dropdownbox what i want to return will be "Vitamin C"
Here is what i have so far:
String myFruits;
List<String> fruits = [
"APPLE",
"ORANGE",
"BANANA",];
DropdownSearch(
onChanged: (dynamic value) {
myFruits = value;
},
mode: Mode.DIALOG,
items: fruits,
),
For now when i print myFruits what it shows is the selected value of the dropbox, what I want is that if pick apple it will return "vitamin c" like that. Thanks :) How can i achieve this?
you can define a Map from fruits and returnedValue like:
Map<String, String> returnedValue = {
"APPLE" : "Vitamin A",
"ORANGE" : "Vitamin C",
"BANANA" : "Vitamin K",
};
and return from this.
all your code like this :
Function(String) returnFunction();
String myFruits;
String myVitamin;
List<String> fruits = [
"APPLE",
"ORANGE",
"BANANA",
];
Map<String, String> returnedValue = {
"APPLE" : "Vitamin A",
"ORANGE" : "Vitamin C",
"BANANA" : "Vitamin K",
};
DropdownSearch(
onChanged: (dynamic value) {
myFruits = value;
myVitamin = returnedValue[value];
returenFunction(myVitamin); // if you want return from this class
},
mode: Mode.DIALOG,
items: fruits,
),

Bind Data to dropdown in MVC and Entity Framework

As of now, dropdown is like this. I want to populate options from database instead of typing. How to do this ? We are using Kendo UI.
<td>
<label for="ln" class="lblTask">Type</label><br />
<select name="type" data-bind="value:type" style="color:black">
<option>CM</option>
<option>CTA</option>
<option>ESP</option>
</select>
</td>
try this:
In view
#Html.DropDownListFor(a => a.SelectedItem , Model.Item)
In Model
public class Items
{
List<SelectListItem> itemList = new List<SelectListItem>();
public List<SelectListItem> item
{
get { return itemList; }
set { itemList = value; }
}
public items()
{
itemList.Add(new SelectListItem() { Value = "1", Text = "CM", Selected = true });
itemList.Add(new SelectListItem() { Value = "2", Text = "CTA" });
itemList.Add(new SelectListItem() { Value = "3", Text = "ESP" });
}
}
Let me know.If it helps.
<input id="type" style="color:black" />
<script>
$(document).ready(function() {
$("#type").kendoDropDownList({
dataTextField: "Name",
dataValueField: "Value",
dataSource: {
transport: {
read: {
dataType: "json",
url:'<%=Url.Content("~/Controller/Action")%>',
}
}
}
});
});
</script>
Considering that your action return jason value as
[{ name: "CM", value: 1 },{ name: "CTA", value: 2 },{ name: "ESP", value: 3 }]
in Controller
public JsonResult ActionName()
{
var List= ...
return Json(List);
}
i hope this will help you

ASP.NET MVC 2: Manually create a list and attach to model

I have a model:
[Required]
public List<SelectList> Meals { get; set;
}
And I want to create a list in my Controller so I can attach it to "Meals" in my Model.
For some reason I'm having issues doing this:
_model.MaxCoupons = new List<SelectListItem>();
SelectListItem _mList = new SelectListItem([]{
new SelectListItem { Text = "---", Value = "" },
new SelectListItem { Text = "50", Value = "50" },
new SelectListItem { Text = "60", Value = "60" },
new SelectListItem { Text = "70", Value = "70" },
new SelectListItem { Text = "80", Value = "80" },
new SelectListItem { Text = "90", Value = "90" },
new SelectListItem { Text = "100", Value = "100" },
new SelectListItem { Text = "110", Value = "110" },
new SelectListItem { Text = "120", Value = "120" },
new SelectListItem { Text = "130", Value = "130" },
new SelectListItem { Text = "140", Value = "140" },
new SelectListItem { Text = "150", Value = "130" } },
"Text", "Value" );
_model.MaxCoupons.Add(_mList);
I'm a bit confused right now...
public SelectList Meals { get; set; }
public SelectList MySelectList()
{
List<SelectListItem> _returnList = new List<SelectListItem>();
SelectListItem _mList = new SelectListItem();
_mList = new SelectListItem(){ Text = "---", Value = "" };
_returnList.Add(_mList);
//keep repeating
}
then inside a method:
Meals = new MySelectList();