Question

ViewData["dropDown_ViewOwnerShare"] = correspondingDropDownValue.Select(j => new SelectListItem { Text = j.ListOfValue, Value = j.ListOfValue, Selected = j.ListOfValue == x.DefaultValue }).ToList();

Base on the above code, the viewdata["dropDown_e"] which is a dropdownlist successfully been generated. And now for each of the element, instead of using j.ListOfValue as Value, I want to use j.UserID as Value. Problem is j.UserID is a long type returned from database and Value is always expecting a string. I've tried j.UserID.ToString() but it has compile error. May I know how should I handle it?

Était-ce utile?

La solution

What is a type of correspondingDropDownValue? Suppose it's IQueryable<T>, right? At first, you need to get all required fields from the database, then cast result to IEnumerable<T> and only then - create SelectListItems list. Something like this:

    correspondingDropDownValue
    .Select(j => new {Value = j.UserID, Text = j.SomeTextField}) // SELECT UserID, SomeTextField FROM SomeTable
    .AsEnumerable()       // Evaluate the query
    .Select(j => new SelectListItem // Work with in-memory objects using LINQ to Objects instead of LINQ to Entities
    {
        Value = j.Value.ToString(),
        Text = j.Text,
        Selected = j.Value == x.DefaultValue
    })
    .ToList()   
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top