Why Use C# Syntax To Create A List Of Links In MVC 2
Sep 3, 2010
I'm having hard time understanding the following C# code. This code was taken from Pro ASP.NET MVC 2 Framework by Steven Sanderson. The code esentially creates URLs based on a list of categories. Here's the code:
A lot of stuff is going on here. I'm guessing it's defining a function that expects two parameters of type string, and NavLink. Then I see the Lambda categoryName => new NavLink etc.... I think all it's doing is creating an instance of NavLink. The function is then called in the same Controller action:
// Place home link on top
List<NavLink> navLinks = new List<NavLink>();
navLinks.Add(makeLink(null));
// Add link for each distinct category
var categories = productsRepository.Products.Select(x => x.Category.Name);
foreach (string categoryName in categories.Distinct().OrderBy(x => x))
navLinks.Add(makeLink(categoryName));
I can tell that it's making a list of NavLink. I don't understand why Steven Sanderson wrote it this way though. Couldn't he have written something like:
var categories = productsRepository.Products.Select(x => x.Category.Name);
foreach (string categoryName in categories.Distinct().OrderBy(x => x))
{
var newlink = new Navlink{
text = categoryName,
RouteValues = new RouteValueDictionary(new {
controller = "Products",
action = "List",
category = categoryName,
page = 1
}),
IsSelected = (categoryName == currentCategory)
}
navLinks.Add(newlink);
}
Is there an advantage to doing it Steven's way versus my way?
Now I want to attach to the click-event of every single li-element, so that if the user clicks on a div-element, detailed product-informationen should be loaded asynchronously into a details-pane. I know how I can use jQuery to invoke an action-method ajax-style and also how to display the json-result which contains the product-details, BUT I have no idea, how I could attach the onclick-event to every single div, so that I can use the productId to load the details.
I was wondering, wether it is possible to use the Asp.net AdRotator to rotate through a List of links.
This would be my business class.
public class AdRotatorData { public String Name {get;set;} public String NavigateURL {get;set;} public String Description {get;set;} }
I would like to bind a List to the AdRotator control and modify the output of the AdRotator control to display the Link rendering with the Name and NavigateUrl and some text from the Description property below.
Is such thing possible? The ComponentArt AdRotator component http://aspnetajax.componentart.com/control-specific/rotator/design/product_scroller/WebForm1.aspx provides such features, but I can't afford the entire control package.
Does anyone have a list of link(s) on the www for a good list of DI gotchas?I have been trying to inject controls using DI, in an asp.net webforms app and found that on recursive build up that ViewState is lost.
Also would be helpful a list of articles where the developer needs to be aware to gotchas before taking teh big step in implementing IoC/DI in the app.
i want to place a bulleted list with links inside a master page. the problem is that no matter what i do i get an error when i try to press on the link when the link directs me to a page that exists on another folder in my project. (i tried using the ~/some_folder/somewebform.aspx but it didn't work).
I add two more pages test1.aspx and test2.aspx. Now I want:
- These two links are children of "About Us".
- When I click test1.apx or test2.aspx the "About Us" title will be highlighted.
- I tried add child node to "<siteMapNode url="aboutus.aspx" title="About Us" description="" />" and it work fine. However, my boss doesn't want me to use this one.
I want to create the structure like following 1. Main Area 1 A. Sub Area 1 B. Sub Area 2 2.Main Area 2 A. Sub Area 1 B. Sub Area 2 Whenever I will click on main area or sub area it should re-direct to that page and also, if add new area in database it should reflect in the structure Also specify me whether should i use sitemap for this?
I need to create tabs for links in my .aspx page. I want the links to be separated out either with vertical lines or tabs. This code is part of my Master page.
We are currently updating our policy page on our intranet. Currently the page is written in ASP and connects to an Excel spreadsheet that serves as an index, which contains policy name and number. We also have a directory that contains all of the policiesnamed the policy number. The control that is currently being used will get all of the policies listed in the index and create links for them. We do not have to manually create the hundreds of links we just update the index. Does anyone have an idea of a control or a way I can accomplish this in asp.net?
I have already stored value of author id in list item value while page loads. but when I change index of drop down list to view author information error is shown. I tried to debug using check points and it seems Sqlreader isn't reading database second time.
how are you able to create a admin and member folder and seperate the links from the masterpage .The login and register is not done using the asp.conf is done using a customer table
I'm having problem in inserting text from a textbox in which user write or copy some text which may contain special characters ( " , . ) etc. If there is any special character it get conflict with CommandText Syntax and generates error in inserting. I want to insert all these characters. How could i do it?
For reporting we used report RDLCs and displayed using an ASPX form. We implemented using the route as shown below. This resulted in forcing us to use the page route below and also to use the hyperlink instead of the normal Action Link. The question is how do we convert the asp:hyperlink ASPX syntax to Razor syntax.
I am working with a sql server database with about 50 stored procedures. The database and stored procedureswere not created by me. The stored procedures all begin with a number in their name.
Example: 17P_Comsetter
The problem I am having is that if I try and execute the stored procedure at the command line in Query Analyzer,the query analyzer appears not to like that the stored procedure begins with a number. For example:
If I try an run the stored procedure as follows:
EXEC 17P_Comsetter
Then I get the following error message:
Msg 102, ..... Incorrect Syntax near '17'
I am using SQL server 2005. Does anyone know why I am getting this error message?
DataRow[] filterRows = ds.Tables[0].Select("Running Status= case State when 'True' then 'Running' When 'False' Then 'Stoped' end Where Active='1' and State='1'");
this is not working showing syntax error syntax error:operand is missing
My question is, is there any benefit to instantiating a new userDetails object in every loop, or can this be created outside the loop and title, forename and surname just written over with every pass of the loop?
How would i create a details view that also contains a list?
My aim is to have a details view that displays information about a person and then have a list on the same page that displays facts about that person.
Currently, my details view is working fine. I just need to add the list part. The details view inherits Models.MyViewModel which is a custom class as the details come from multiple tables.
how to make a list of titles of news contained in a database. And in this list, each of the titles when clicked is redirected to a page where you will be able to view the news in full (Title, Body, Author ..). What I got:
- A database containing a table with the news, every news is associated with an identification code (ex: "ID").
- A page where you will make the listing. (Ex: site / listofnews.aspx)
- I have a page that uses the method "querystring" to know what is the primarykey the news. (Ex: site/shownews.aspx?ID=12345, where "12345" is the primarykey of the news. Once it knows what is the primarykey of the news in the database, it loads each of the fields of the page (news.aspx) with the news, this part is working ok.
- The data is retrieve using the Linq, so I receive a List of "News", the class "News" as ID, Title, Body, Author..
how to make the listing clickable.
In php I used this method (make a list of html links, in each link the href field is changed so that the tag "id" coincides with the news):
//database used is oracle, $stmt is the query, you donīt need to understand how it works. oci_define_by_name($stmt, "ID", $id); oci_define_by_name($stmt, "TITLE", $title); if (!oci_execute($stmt, OCI_DEFAULT)) {........