I had to change on my lines of code around. I before had something like this
// this is in a static method.
List<string> mySTring = new List<string>();
mySTring.add("one");
mySTring.add("two");
However on one of my pages I have a dropdownlist that does not require the field "two" so instead of writing duplicate code all I did was
myString.remove("two");
Now I need to change my list to a List<SelectListItem> myList = new List<SelectListItem>();
So I have it now looking like this:
List<SelectListItem> myList = new List<SelectListItem>()
{
new SelectListItem() { Text = "one", Value = "one"},
new SelectListItem() { Text = "two", Value = "two"},
};
So now how do I remove the selectListItem that contains "two"? I know I probably could use remove by index. But I might add to list in the future so I don't want to start hunting down and changing it if the index changes.
Thanks
List<T> is, by default, going to be comparing object references (unless SelectListItem implements a custom equality method). So unless you still have a reference to the second item around, you are going to have to get it either by reference, or by finding the desired item:
var item = myList.First(x=>x.Value == "two");
myList.Remove(item);
Index may be easier...
You could use the RemovalAll method:
myList.RemoveAll(i => i.Text == "two");
Obviously this will get rid of all the items whose "Text" property is "two", but since you're using it in a ComboBox I'm assuming you'll only have one item for each "Text" value.
we can make mouse selection over the autocomplete div by adding this following code
into the jquery autocomplete function
here i have used the id name as Requiredid
$("#Requiredid").autocomplete({focus:function(e,ui) {
document.getElementById('Requiredid').value = ui.item.label;
document.getElementById('hdnValue').value = ui.item.id;//If You need this value you //can add this line
}});
Related
In my recent project im trying to make a dictionary with key as a string and value as List of string(List) and adding value in dictionary using for loop ,
but the problem is that after first iteration when I Update the List for second iteration it is automatically changing in the first key value pair.
for example in first iteration it is saving key as apple and value as list {cucumber,chilli,tomato,apple} its fine but after first iteration when i update list to {cucumber,chilli,tomato,apple,mango} and saving it to second key mango it is also updating the first value to {cucumber,chilli,tomato,apple,mango}.
var mylist = new List<string>()
{
"cucumber",
"chilli",
"tomato"
};
var yourlist = new List<string>()
{
"apple",
"mango",
"banana"
};
var dict = new Dictionary<string, List<string>>();
foreach (var i in yourlist)
{
mylist.Add(i);
dict.Add(i,mylist);
}
foreach(var d in dict.Keys)
{
foreach(var l in dict[d])
{
Console.WriteLine(l);
}
}
The dictionary entries' Value properties are always the same list, so anything you do to one, ends up showing in all of them (because there is only one)
Take a look at the code below; if you understand why a and b here both show the same change, then you should understand that your dictionary scenario is essentially the same
var list = new List<string>(){ "hello", "world" };
var a = list;
var b = list;
a.Add("there");
Console.Write(b.Count); //shows 3
If you don't understand why a, b and list above all refer to the same list, then drop a comment and I'll add some more explanation
As to what you should do about your "issue", it's not entirely clear to me what you're hoping to do but if you made sure that each key in the dictionary associated with a new list rather than the same one, then changes to one key's list would not show in other keys' lists:
dict.Add(i, new List<string>(mylist));
This makes a new list per key, and initializes the new list with the items present in mylist at the time (my list grows each pass of the loop)
I am working to create a view that contains a dropdownlist. To generate the select list items, I wrote a method to return the list of SelectListItem desired for this dropdown - one of which has an attribute of Selected set to true.
I have traced through the method generating the IEnumerable<SelectListItem> representative of the options I wish to render, and it is returning the desired result.
However when I use Html.DropDownList() utilizing the IEnumerable<SelectListItem> returned above, my rendered select element
has an initial blank option which does not appear in the DOM. Selecting any option will remove it, but I am confused as to why
the selected option from the List<SelectListItem> is not honored.
Static method generating list of SelectListItem:
public static IEnumerable<SelectListItem> GetDropDownValues()
{
IEnumerable<MyClass> myClassesForList = MyClass.GetItemsForList();
List<SelectListItem> retVal = new List<SelectListItem>();
retVal.Add(new SelectListItem() { Text = "Choose", Value = "0", Selected=true });
IEnumerable<SelectListItem> myClassesSelectListItems =
from x in myClassesForList
select new SelectListItem
{
Text = x.Name,
Value = x.Value
};
return retVal.Concat(myClassesSelectListItems);
}
Pertinent Razor snippet:
#Html.DropDownList("dropDownVals", GetDropDownValues())
Edit 1: Screenshot of resulting drop-down list
Make sure there's not a viewbag/viewdata named dropDownVals, which holds a value that doesn't exist in the list returned by the method GetDropDownValues
What if you use AddRange instead of Concat:
retVal.Add(new SelectListItem() { Text = "Choose", Value = "0", Selected=true });
var myClassesSelectListItems = from x in myClassesForList
select new SelectListItem
{
Text = x.Name,
Value = x.Value
};
retVal.AddRange(myClassesSelectListItems);
return retVal;
I have a combobox and I see that I am not able to set SelectedValue like this:
cmbA.SelectedValue = "asd"
So I tried to do this
cmbA.SelectedIndex = cmbA.FindString("asd");
Based on How to set selected value from Combobox?
I realised that my combobox is a System.Windows.Controls.ComboBox and not a System.Windows.Forms.ComboBox.
That means that FindString() is not available.
Based on User Control vs. Windows Form I get that forms are the container for controls, but I dont get why the Controls.ComboBox does not implement FindString().
Do I have to write my own code to do what FindString() does for Forms.ComboBox?
WPF ComboBoxes are not the same as WinForms ones. They can display a collection of objects, instead of just strings.
Lets say for example if I had
myComboBox.ItemsSource = new List<string> { "One", "Two", "Three" };
I could just use the following line of code to set the SelectedItem
myComboBox.SelectedItem = "Two";
We're not limited to just strings here. I could also say I want to bind my ComboBox to a List<MyCustomClass>, and I want to set the ComboBox.SelectedItem to a MyCustomClass object.
For example,
List<MyCustomClass> data = new List<MyCustomClass>
{
new MyCustomClass() { Id = 1, Name = "One" },
new MyCustomClass() { Id = 2, Name = "Two" },
new MyCustomClass() { Id = 3, Name = "Three" }
};
myComboBox.ItemsSource = data;
myComboBox.SelectedItem = data[0];
I could also tell WPF I want to consider the Id property on MyCustomClass to be the identifying property, and I want to set MyCombbox.SelectedValue = 2, and it will know to find the MyCustomClass object with the .Id property of 2, and set it as selected.
myComboBox.SelectedValuePath = "Id";
myComboBox.SelectedValue = 2;
I could even set the Display Text to use a different property using
myComboBox.DisplayMemberPath = "Name";
To summarize, WPF ComboBoxes work with more than just Strings, and because of the expanded capabilities, FindString is not needed. What you are most likely looking for is to set the SelectedItem to one of the objects that exist in your ItemsSource collection.
And if you're not using ItemsSource, then a standard for-each loop should work too
foreach(ComboBoxItem item in myComboBox.Items)
{
if (item.Content == valueToFind)
myComboBox.SelectedItem = item;
}
I don't know what you are trying to do but I think it would be easier to just do
cmbA.Text = "String";
That way you get your selected item
Else I found an intersting article that could help you out:
Difference between SelectedItem, SelectedValue and SelectedValuePath
I have this code and I would like to add Item in the list and I am doing like this.
IEnumerable<SelectListItem> note = new List<SelectListItem>();
var selectList = new SelectListItem
{
Text = Convert.ToString(amount.Key),
Value Convert.ToString(amount.Value)
};
note.ToList().Add(selectList);
I am unable to Add item.
The ToList() call creates a brand new list, which is not connected to the original list. No changes you make to the new list will be propagated back to the original.
As for adding, you cannot add items to the IEnumerable<T>. It all depends on that you're trying to do with your code, but why do you use IEnumerable<SelectListItem> instead of IList<SelectListItem>, which has an Add() operation. Since IList<T> is-a IEnumerable<T>, you will be able to pass the note to any method that expects an IEnumerable<T>.
When you say ToList() it's not casting it, it's creating a whole new list and that's what you're adding to.
What you want is to avoid the weird casting you do to start with (make note a List not an IEnumerable) and then add to that directly
List<SelectListItem> note = new List<SelectListItem>()
var selectList = new SelectListItem{Text = Convert.ToString(amount.Key), Value Convert.ToString(amount.Value) }
note.Add(selectList)
Not that it makes any sense to do that but if you really want to assign your list to an IEnumerable to begin with you can still cast it later when you need the added functionality as follow:
IEnumerable<SelectListItem> note = new List<SelectListItem>()
var selectList = new SelectListItem{Text = Convert.ToString(amount.Key), Value Convert.ToString(amount.Value) }
((List<SelectListItem>)note).Add(selectList)
List<SelectListItem> note = new List<SelectListItem>();
var selectList = new SelectListItem
{
Text = Convert.ToString(amount.Key),
Value Convert.ToString(amount.Value)
};
note.Add(selectList);
var notes = note.AsNumerable();
the two lists to be passed to the view from controller.
ViewBag.mylist1 = new Accounts().List(Category.Income);
ViewBag.mylist2 = new Accounts().List(Category.Expenditure);
HOw to combine thse two lists in a single view?
myList1 = myList1.Concat(myList2).ToList();
Concat returns an IEnumerable that is the two lists put together, it doesn't modify either existing list. Also, since it returns an IEnumerable, if you want to assign it to a variable that is List, you'll have to call ToList() on the IEnumerable that is returned.
You can use also AddRange()
ViewBag.myList1 = ViewBag.myList1.AddRange(ViewBag.myList2);
AddRange() modifies the list by adding the other items to it.
Concat() returns a new sequence containing the list and the other items, without modifying the list.
List<SelectListItem> lstFuelType = new List<SelectListItem>();
var FuelType = dbContext.APPL_COMMON_MST.AsNoTracking().AsQueryable().Where(cm);
lstFuelType = new SelectList(FuelType, "KEY", "VALUE").ToList();
lstFuelType.Insert(0,new SelectListItem { Text = "ALL", Value = "ALL", Selected = false });
ViewBag.FuelType = lstFuelType;