Remove empty Dictionary entry - c#

I'm trying to make a Price Range filter as you can see live here.
It's made in Umbraco 7.
I would like to hide a price range IF there is NO products in the range.
My code looks like this:
string selectedpriceRange = queryString.Get("priceRange");
<ul id="priceFilter" class="section sidebarList">
<li #(String.IsNullOrEmpty(selectedpriceRange) ? currentClass : "")>
All prices
</li>
#{ var priceRanges = new Dictionary<int, int>();
priceRanges.Add(0, 4999);
priceRanges.Add(5000, 9999);
priceRanges.Add(10000, 14999);
priceRanges.Add(15000, 24999);
priceRanges.Add(25000, 34999);
priceRanges.Add(35000, 44999);
priceRanges.Add(45000, 54999);
priceRanges.Add(55000, 95000);
}
#foreach (KeyValuePair<int, int> range in priceRanges)
{
<li>
<a href="#CurrentPage.Url#queryStringBuilder("priceRange", string.Format("{0}-{1}", range.Key, range.Value))">
#string.Format("{0} - {1}", range.Key, range.Value)
</a>
</li>
}
</ul>
I would like to think it's possible to add a .where or Count in the new Dictionary to see IF there is anything in that price range, but I'm not sure.

Related

Department structure treeview passing argument to fetch employees (MVC 4)

I have the following problem: I am making an MVC intranet website for the corporation I'm working for. One part of the job is to make a phonebook - I need a tree like structure of the departments (with depth).
I have a view with two div elements - left (containing the departments, the structure follows below), and a right div which should show all the employees that are working in the selected (clicked) department.
#helper GetTree(List<PhonesClasses.Department> department, int parentID){
foreach(var i in department.Where(a=>a.headDepartmentID.Equals(parentID)))
{
{var childDepartments = department.Where(a => a.headDepartmentID.Equals(i.departmentID)).Count();
if(childDepartments > 0)
{
<li class="haschlid" id="#i.departmentID">
#i.departmentName
<ul class="sub-dep">
#GetTree(department, #i.departmentID)
</ul>
</li>
}
else
{
<li id="#i.departmentID">
#i.departmentName
</li>
}
}
}
The following is the above-mentioned view. As you can see, I had the idea to make a partial view but I'm not sure I'm headed in the right direction.
<div class ="containerStructure">
<div class="leftDivStructure">
#if (Model != null && Model.Count() > 0)
{
<ul class="list" id="deplist">
#Treeview.GetTree(Model, Model.FirstOrDefault().headDepartmentID)
</ul>
}
</div>
<div class="rightDivStructure">
Employee
#*#Html.Partial("_PeopleInDepartment", new {depID = Model.departmentID()})*#
</div>
</div>
My employee and department classes both have DepartmentID fields, so when a department is clicked on in my tree view, a parameter () should be passed to the partial view, or whatever needs to be there to handle the parameter and show the employees. Below is the controller that I think has to fetch the result.
public ActionResult PeopleInDepartment(int depID)
{
List<Person> peopleList = new List<Person>();
peopleList = Persons.GetPersons(depID);
return View(peopleList);
}
For further clarifications please comment!

How to dynamically create several ActionLinks from the Controller in MVC?

I'm using MVC where I have a list of strings that I would like to point to a new page. I'm using Razor and am very new to MVC and cannot seem to find the answer to my question through google.
My list could contain of the following:
"hello"
"goodbye"
"seeya"
I know how to insert strings from the controller to the html page using ViewBag, and I would use the following actionlink if I had a fixed set of strings:
#Html.ActionLink("viewedName", "ChemicalClass", new { mystring = "hello" })
#Html.ActionLink("viewedName", "ChemicalClass", new { mystring = "goodbye" })
#Html.ActionLink("viewedName", "ChemicalClass", new { mystring = "seeya" })
As I understand it, this would generate 3 links that would redirect to the subpage "ChemicalClass", and it would contain the one of the 3 parameters, depending on the link that was clicked.
My quesiton is, how can I do the same, but have the ActionLinks created dynamically, since I won't know how many links are going to be created, nor the content of the strings. My goal is to show these links on the webpage in (preferabely) a list form, e.g.:
<ol>
<li>
hello
</li>
<li>
goodbye
</li>
<li>
seeya
</li>
</ol>
Where each element in the list is a link and not just a string.
Create a view model that stores a collection of links
Model
public class ViewModel
{
public IList<string> Links { get; set; }
}
populate that model in your controller
Controller
public ActionResult Index()
{
var model = new ViewModel
{
Links = new List<string>
{
"Hello",
"Goodbye",
"Seeya"
}
};
return View(model);
}
and finally your view
View
#model MvcApplication1.Models.ViewModel
<ol>
#foreach (var item in Model.Links)
{
<li>
#Html.ActionLink("viewedName", "ChemicalClass", new { mystring = item })
</li>
}
</ol>
Your class holds your collection of strings and razor loops over them to produce your links.
you can use something like this.
<ul>
#foreach (var x in Model)
{
<li>#x.myString </li>
}
</ul>
I have something like this in one of my Views:
#foreach (var item in Model.MyList)
{
<li>
#Html.ActionLink("Select", "ActionName", "Chemical", new {id = item.AdmNum}, new { #class = "label label-info"})
</li>
}

How can I make sure that the newest items added to the list variable gets placed in the top and not in the bottom when it get displayed in html?

I am using MVC + EF
I have a Feed xml file url that gets updated every 7 minute with items, every time a new item gets added I retrieve all the items to a list variable and then I add these varible to my database table. After that I fill a new list variable which is my ViewModel from the database table. Then I declare the ViewModel inside my view which is a .cshtml file and loop throught all of the objects and display them.
How can I make sure that the newest items get placed on the top and not in the bottom?
This is how I display the items inside my cshtml note that I use a ++number so the newest item needs to be 1 and so on.. :
#model Project.Viewmodel.ItemViewModel
#{
int number = 0;
}
}
<div id="news-container">
#foreach (var item in Model.NewsList)
{
<div class="grid">
<div class="number">
<p class="number-data">#(++number)</p>
</div>
<p class="news-title">#(item.Title)</p>
<div class="item-content">
<div class="imgholder">
<img src="#item.Imageurl" />
<p class="news-description">#(item.Description) <br />#(item.PubDate) | Source</p>
</div>
</div>
</div>
}
</div>
This is how I fill the viewmodel which I use inside the .cshtml file to iterate throught and display the items
private void FillProductToModel(ItemViewModel model, News news)
{
var productViewModel = new NewsViewModel
{
Description = news.Description,
NewsId = news.Id,
Title = news.Title,
link = news.Link,
Imageurl = news.Image,
PubDate = news.Date,
};
model.NewsList.Add(productViewModel);
}
Sorry for the paint lol :P
Any kind of help is appreciated
#foreach (var item in Model.NewsList.OrderByDescending(n => n.PubDate)

Create a list to display the contents of a dictionary?

I have a dictionary that i would like to display as a list on my view page. At the moment i can create a list but i cant set a dynamic size or populate it with my dictionary. I would like to do something like a foreach, or if there is a faster just to list function?
Im new to MVC sorry for poor code.
#model Dictionary<string, string>
#{
ViewBag.Title = "Download";
}
<hgroup class="title">
<h1>#ViewBag.Title.</h1>
<h2>#ViewBag.Message</h2>
<br>
#{
var filelist = #Model.ToList();
}
<Div>
<select name="Fileslist" size="5">
<option>Count</option>
<option>text2</option>
<option>text3</option>
<option>text4</option>
<option>text5</option>
</select>
</Div>
</hgroup>
A simple Dictionary to List would be
Dictionary<string, string> myDictionary = new Dictionary<string, string>();
// Add some values in dictionary
var res = myDictionary.Select(x => x.Key + "-" + x.Value).ToList()
You can use #Html.DropDownListFor which supports Dictionary directly.
Take a look at this answer for more details.

How do I get the children of an Element in Watin?

Given the following html:
<ul id="search-results">
<li>
<h3>Result 1</h3>
<span class="some-info">Tag line</span>
</li>
<li>
<h3>Result 2</h3>
<span class="some-info">Another bit</span>
</li>
<li>
<h3>Result 2</h3>
<span class="some-info">Another bit</span>
</li>
</ul>
I can get the ul element with:
Element ul = ie.Element(Find.ById("search-results"));
How do I iterate over the children of the search-results?
I've managed to get as far as:
var allListItems = ie.Elements.Filter(e => e.Parent != null && e.Parent.Id == "search-results");
But this doesn't help me assert things about the H3 or span contained within the li's.
*Option1:*Based on your code, you can try something like:
ie.Spans.Filter(Find.ByClass("some-info"))[0...2].PreviousSibling;
you will need to loop over each span.
*Option2:*Based on some suggestions from the Watin mail list (similar to your own answer as well):
IElementContainer elem = (IElementContainer)ie.Element(Find.ById("search-results"));
Then you parse through elem.Element
Current best hack I can think of is:
public static class WatinExtensions
{
public static ElementCollection Children(this Element self)
{
return self.DomContainer.Elements.Filter(e => self.Equals(e.Parent));
}
}
ElementCollection results = ie.Element(Find.ById("search-results")).Children();
// and
foreach(Element li in results)
{
Element info = li.Children().First(e => e.ClassName.Contains("some-info"));
}
It works but surely there is a proper why to do this?
And there is a correct way to do this from the watin mailing list:
In my example:
var searchResults = (IElementContainer) ie.Element(Find.ById("search-results"));
foreach (Element element in searchResults.Elements) {
// can do more here
}

Categories

Resources