开发者

How do I attach a link (to a View) to an image in ASP.NET MVC?

Ok, so here is my situation. I am creating a web application using ASP.NET MVC 2 using the C# language. I have programmed in HTML, CSS, and PHP for several years and I am very new to ASP.NET. The part that I am having trouble with is the image gallery.

The setup: I have a link on the navigation bar that goes to a "Galleries" page. This page will show a list of galleries. Each gallery has a title, an image, and a description. All of this information is pulled from an XML file. I'm using the XML file like a database. I wanted to use this method开发者_开发技巧 so that i could easily update the list of galleries and have the updated XML file automatically be reflected by the website. Now, the galleries should link to an "Images" page. This page will display a list of images within the gallery based on what gallery was selected. This page will also pull from an XML file.

The problem: I cannot seem to attach a dynamic link to the image? I am also stuck and not sure how to get the correct View to display. I know I need to do something with the controllers and models, right? I have some code if needed? I would greatly appreciate any help or direction for this! Thanks!


For the image, as long as you have the collection of images in the Model for the page, you can loop through each element and do something like:

<a href="<%= Html.Encode(image.Url) %>" />

When it comes to the Views, there's some auto-magic that happens behind the scenes to map you Controller to the View.

Scott Guthrie has a good post on the inner workings of the ASP.NET MVC Framework and the different methods that it uses to go from your Controller to your Views:

ASP.NET MVC Framework (Part 1) - ScottGu's Blog


I would much rather create an Html Helper for that. That way you can write something more along the lines of this:

<%= Html.ImageLink("<url to image">) %>

Feels like a cleaner solution to me then having to write out an anchor tag. Either way will work, though.

For more info on writing custom Html Helpers:

http://www.asp.net/Learn/MVC/tutorial-09-cs.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜