Camillia
asked on
Rewriting this code another way
I'm so stuck on this. I know what the error is but I don't know how to fix it so I'm trying to rewrite it another way.
(this is the error : Unrecognized expression node: ListInit )
The code below gives me a LINQ error. The solution is here but I don't know how to fix it Solution to error
1. This gives me an error:
2. So, I rewrote it this way but this isn't working because I init the list
var myList = new List<SelectListItemWithAtt ributes>() ;
and this comes out as empty
And this is the definition of list:
(this is the error : Unrecognized expression node: ListInit )
The code below gives me a LINQ error. The solution is here but I don't know how to fix it Solution to error
1. This gives me an error:
public List<SelectListItemWithAttributes> DropDownDatas
{
get
{
DataClasses1DataContext db = new DataClasses1DataContext();
var DropDownDataItems = (
from c in db.states
select new SelectListItemWithAttributes()
{
Text = c.Name,
Value = c.Id.ToString(),
HtmlAttributes = new Dictionary<string, string> { { "data-countryid", c.country.ToString().ToLower() } }
}).ToList();
DropDownDataItems.Insert(0, new SelectListItemWithAttributes() { Text = "-- Select --", Value = "", HtmlAttributes = new Dictionary<string, string> { { "data-callback", "false" } } });
return DropDownDataItems;
}
}
2. So, I rewrote it this way but this isn't working because I init the list
var myList = new List<SelectListItemWithAtt
and this comes out as empty
public List<SelectListItemWithAttributes> DropDownDatas
{
get
{
DataClasses1DataContext db = new DataClasses1DataContext();
var s = from c in db.states
select c;
var myList = new List<SelectListItemWithAttributes>(); //this is wrong
foreach (var row in s)
{
foreach (var item in myList)
{
item.Text = row.Name;
item.Value = row.Id.ToString();
item.HtmlAttributes = new Dictionary<string, string>
{
{"data-countryid", row.country.ToString().ToLower()}
};
myList.Add(item);
}
}
return myList;
}
}
}
And this is the definition of list:
public class SelectListItemWithAttributes : SelectListItem
{
public IDictionary<string, string> HtmlAttributes { get; set; }
}
ASKER
Thank you so much for taking a look. I've been trying to get this done all afternoon. I was about to try something else.
I need this back in there. I think this is what throws the error
item.HtmlAttributes = new Dictionary<string, string>
{
{"data-countryid", row.country.ToString().ToL ower()}
};
I need this back in there. I think this is what throws the error
item.HtmlAttributes = new Dictionary<string, string>
{
{"data-countryid", row.country.ToString().ToL
};
ASKER
Yes, this line causes the error. I just took it out and it runs without but i need that back.
In this code. It's a LINQ error so there has to be a different way of doing this..
HtmlAttributes = new Dictionary<string, string> { { "data-countryid", c.country.ToString().ToLower() } }
In this code. It's a LINQ error so there has to be a different way of doing this..
var DropDownDataItems = (
from c in db.states
select new SelectListItemWithAttributes()
{
Text = c.Name,
Value = c.Id.ToString(),
HtmlAttributes = new Dictionary<string, string> { { "data-countryid", c.country.ToString().ToLower() } }
}).ToList();
I tried what I gave you using a database and it populates the drop box. I am kind of new to LINQ.
So, populating the control works with a data table. I am assuming your coding upto db.states is correct. If it is then it should work for you as well.
Did you try it as I had it? Please give it a try.
Mike
So, populating the control works with a data table. I am assuming your coding upto db.states is correct. If it is then it should work for you as well.
Did you try it as I had it? Please give it a try.
Mike
ASKER
Yes, as you have it works. But I need that line of code back. That's what breaks it. I'll continue my search for an answer.
Is it suppose to add something to the drop box? if so, how the final data should look like?
ASKER
it should look like this
<option value="1" data-countryid="US">MO</option>
<option value="1" data-countryid="US">MO</option>
ASKER
I tried it like this but got an error that index must be within the bounds of list.
var s = from c in db.states
select c;
var myList = new List<SelectListItemWithAttributes>();
foreach (var row in s)
{
myList.Insert(row.Id, new SelectListItemWithAttributes() { Text = row.Name, Value = row.Id.ToString(), HtmlAttributes = new Dictionary<string, string> { { "data-ccountryid", row.country } } });
}
Can you add this dictionary data to your db.states before hand (before it gets to LINQ)? It seems you are trying to add two fields to the drop box which I think will not work.
I am testing something new, brb
I am testing something new, brb
ASKER
I have it in db.states. I need to add it to the dropdown as an extra attribute. It's ok. Someone else might be able to look at this question.
my dictionary was called listBoxes, change it to whatever you have. But it seems your dictionary is anonymous.
This code comes after the first one. Remove ">" from code. I wanted to have a visual confirmation items added.
FYI, I will away for a while.
Mike
foreach (var d in listBoxes)
{
DropDownList1.Items.Insert(0, ">" + d.Value.ToString());
}
This code comes after the first one. Remove ">" from code. I wanted to have a visual confirmation items added.
FYI, I will away for a while.
Mike
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that might work I'll try it and add the custom htmlattribute back and see
DataClasses1DataContext db = new DataClasses1DataContext();
ListItem[] s = (from c in db.states
select new ListItem(c.StateID, c.State)).ToArray();
ddllist.DataSource =s;
ddllist.DisplayMember="Ent erFieldNam eHere"; //like "State"
ddllist.ValueMember="Enter RecordIDFi eldHere"; // like "StateID"
?
With ddllist.Items.Add it adds a single item, but with ddllist.Datasource =Table1, it add both items (ID and Name).
ListItem[] s = (from c in db.states
select new ListItem(c.StateID, c.State)).ToArray();
ddllist.DataSource =s;
ddllist.DisplayMember="Ent
ddllist.ValueMember="Enter
?
With ddllist.Items.Add it adds a single item, but with ddllist.Datasource =Table1, it add both items (ID and Name).
Open in new window