I want to create selectlist with items from database grouped by RoomId.
var beds = service.GetApartmentBed(parentId)
.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = localizerBedsType[x.BedType.ToString()],
Group = new SelectListGroup { Name = x.RoomId.ToString()},
}).ToList();
AvailableBeds = new SelectList(beds,
nameof(SelectListItem.Value),
nameof(SelectListItem.Text),
BedId,
nameof(SelectListItem.Group.Name));
But when i want to test it in browser, its shows this error:
Image
This is value of item:
Image
but when i remove last parametr of AvailableBeds it's work well but I don't have groups.
Ok, ive done it. Ive need to change last line to "Group.Name"
AvailableBeds = new SelectList(beds,
nameof(SelectListItem.Value),
nameof(SelectListItem.Text),
BedId,
"Group.Name");
Related
ViewBag.Company= new SelectList(db.i_Company(null).OrderBy(c => c.CompanyName), "CompanyID", "CompanyName");
This ViewBag contains CompanyID, and CompanyName, but I would like to add an additional line for the default selection. I would like to add something like:
CompanyID = 0 and CompanyName= "All Companies"
to the list I have.
How can I add a line manually to this ViewBag?
You first need to create instance of SelectList separately and then insert a new item to 0 the position in it and then assign it to the ViewBag..
Consider doing following.
List<SelectListItem> items = db.i_Company(null).OrderBy(c => c.CompanyName).Select(o => new SelectListItem { Value = o.CompanyID, Text = o.CompanyName}).ToList();
items.Insert(0, (new SelectListItem { Text = "All Companies", Value = "0" }));
ViewBag.Company = new SelectList(items);
i'm fairly new to MVC and entity framework and I was hoping someone would be able to help me.
I have a SQL database that has a column that contains ints. I want to pull these values out and put them into an html.dropdownlistfor
I was originally trying to do:
List<SelectListItem> List = dbAccess.KeywordCorrelationData.Select(
temp => new {
Value = temp.DefaultDestroyPeriod.ToString(),
Text = temp.DefaultDestroyPeriod.ToString()
}).OrderBy(temp => temp.Value).Distinct().ToList();
However this only sorted by the text value not numeric value.
I've since found a solution but it's a lot longer:
List<SelectListItem> List= new List<SelectListItem>();
List<int> tempIntList = dbAccess.KeywordCorrelationData.Select(temp => temp.DefaultDestroyPeriod).Distinct().ToList();
tempIntList.Sort();
foreach (int tempInt in tempIntList)
{
int wholeYears = (int)Math.Round((double)tempInt / 365, 0);
SelectListItem newItem = new SelectListItem()
{
Value = tempInt.ToString(),
Text = tempInt.ToString() + " days (approx. " + wholeYears.ToString() + (wholeYears == 1 ? " year)" : " years)")
};
List.Add(newItem);
}
return List;
I was wondering if there was a simpler way to do it than this? I've googled and searched on here but can't find anything like it.
Thanks
Assuming that DefaultDestroyPeriod is already of numeric type, just apply the ordering before you do the Select:
List<SelectListItem> List = dbAccess.KeywordCorrelationData
.OrderBy(kcd => kcd.DefaultDestroyPeriod)
.Select(kcd => new
{
Value = kcd.DefaultDestroyPeriod.ToString(),
Text = kcd.DefaultDestroyPeriod.ToString()
}).Distinct().ToList();
i could solved it just by for loops this way
for loop iterate in datasource and rest of the code as follows
ListItem test = new ListItem { Text = srText, Value = srValue}
test.Attributes.Add("data-imagesrc", "xxx");
test.Attributes.Add("data-description", "xxx");
dropListUserImages.Items.Add(test);
may be there could be error in code because i have composed the above code in notepad. i guess my above code approach will solve my problem but just wondering that can i do the same with LINQ.
here i am giving a sample code which populating dropdown by LINQ but not sure that below code will allow us to add multiple attribute value coming from db?
below sample code taken from http://stackoverflow.com/questions/14710841/making-drop-down-list-in-asp-net-by-foreach-loop
var source = Enumerable.Range(1, 1000)
.Select(i => new { Text= i.ToString(), Value=i.ToString() });
test.DataSource = source;
test.DataTextField = "Text";
test.DataValueField = "Value";
test.DataBind();
looking for guidance to achieve the same with LINQ if possible. thanks
You cannot use the DataSource + DataBind approach if you want to add custom attributes. But why don't you use a simple loop?
var source = db.TableName.Take(1000) // if you only want 1000
.Select(sr => new {
srText = sr.srText,
srValue = sr.srValue,
imagesrc = "xxx", // change
description = "xxx" // change
});
foreach(var x in source)
{
ListItem test = new ListItem { Text = x.srText, Value = x.srValue };
test.Attributes.Add("data-imagesrc", x.imagesrc);
test.Attributes.Add("data-description", x.description);
dropListUserImages.Items.Add(test);
}
I am using MVC4 and what I want to do is use a dropdown connected to my search box to search for the selected property. How would I am stuck on the Text= prop.Name. How could I go through and access all of the properties using this.
My Controller
public ActionResult SearchIndex(string searchString)
{
var selectListItems = new List<SelectListItem>();
var first = db.BloodStored.First();
foreach(var item in first.GetType().GetProperties())
{
selectListItems.Add(new SelectListItem(){ Text = item.Name, Value = selectListItems.Count.ToString()});
}
IEnumerable<SelectListItem> enumSelectList = selectListItems;
ViewBag.SearchFields = enumSelectList;
var bloodSearch = from m in db.BloodStored
select m;
if (!String.IsNullOrEmpty(searchString))
{
bloodSearch = bloodSearch.Where(s => string.Compare(GetValue(s, propertyName), searchString) == 0);
}
return View(bloodSearch);
}
The selectlist is working now I just need to go over my searchstring and how to pass two parameters now.
I'm not quite sure what you're asking. If you want to create a list of objects with the property Text set to the property name of the object, you could get the first object in the BloodStored enumerable and create a list of anonymous types:
// Get one instance and then iterate all the properties
var selectListItems = new List<object>();
var first = db.BloodStore.First();
foreach(var item in first.GetType().GetProperties()){
selectListItems.Add(new (){ Text = item.Name});
}
ViewBag.SearchFields = selectListItems;
I have a function which searches some articles in the Sitecore content items and give me the value. So far I have build up my indexes and it is showing in my IndexViewer. But the return of the function is 0. I looked up this link: http://sitecoregadgets.blogspot.com/2009/11/working-with-lucene-search-index-in_25.html for more information.
protected IEnumerable<Item> ShowHomePageNews(int numOfArticles, string stringofCountries)
{
List<Item> items = new List<Item>();
Sitecore.Search.Index indx = SearchManager.GetIndex("newsArticles");
using (IndexSearchContext searchContext = indx.CreateSearchContext())
{
var db = Sitecore.Context.Database;
CombinedQuery query = new CombinedQuery();
QueryBase catQuery = new FieldQuery("countries", stringofCountries); //FieldName, FieldValue.
SearchHits results = searchContext.Search(catQuery); //Searching the content items by fields.
SearchResultCollection result = results.FetchResults(0, numOfArticles);
foreach (SearchResult i in result)
{
items = result
.Where(r => !r.Title.StartsWith("*"))
.Select(r => db.GetItem(new Sitecore.Data.ItemUri(r.Url).ToDataUri()))
.ToList();
//Lucene.Net.Documents.Field url = i.Document.GetField("_url");
//Sitecore.Data.ItemUri itemUri = new Sitecore.Data.ItemUri(url.StringValue());
//Sitecore.Data.Items.Item item = Sitecore.Context.Database.GetItem(itemUri.ToDataUri());
//items.Add(item);
}
}
return items;
}
Over here the result is 0. What I am doing wrond here?
This is the snapshot of what I am seeing in my IndexViewer:
EDIT:
I am passing a "NZ" in the 'catQuery' and I am getting the result back. Because in my index viewer I am seeing the Field Name = _name, which contains NZ in it. I got this part. However, I want my every field to be indexed. I am seeing only 3 fields in my IndexViewer: _url, _group & _name.
So your countries should be tokenized by the indexer. As a multilist, they will be tokenized by GUID. Searching for a single country by GUID with your code above should work. However, if you want to search for multiple countries, where any of the passed in countries can trigger a match, you need to structure your query differently.
CombinedQuery query = new CombinedQuery();
//apply other filters here to query if need be
//and country filter by creating a new clause (combinedquery) and "ORing" within it (QueryOccurance.Should)
CombinedQuery query3 = new CombinedQuery();
//here you would actually iterate over your country list
query3.Add(new FieldQuery("countries", country1GUID), QueryOccurance.Should);
query3.Add(new FieldQuery("countries", country2GUID), QueryOccurance.Should);
query.Add(query3, QueryOccurance.Must);