asp.net mvc: How to image map?

青春壹個敷衍的年華 提交于 2019-12-10 10:50:01

问题


How in ASP.NET MVC would I construct an image map? For ref:

<map id='headerMap'>
    <area shape='rect' href="Default.aspx" coords='300,18,673,109' />
</map>

One answer of an answer of an unrelated question by markus is something similar:

<a href="<%= Url.RouteUrl("MyRoute", new { param1 = "bla", param2 = 5 }) %>">
   put in <span>whatever</span> you want, also <img src="a.gif" alt="images" />.
</a>

Sorry if this is redundant. My research indicated that this may be a version 2 mvc answer. Looking for something similar to Html.ActionLink if it exists. Obviously, I could reference the route by name and send in the parameters using that Url.RouteUrl, but is this the defacto way to handle it?

Thanks


回答1:


You'll have to create the HTML yourself... have a look at the html that is render in classic asp.net using:

<map id='headerMap'>
    <area shape='rect' href="Default.aspx" coords='300,18,673,109' />
</map>

Then mimic that in your own asp.net mvc view replacing any of the hrefs for the map with your Url.RouteUrl calls.

E.g.

<map id="mymap" name="mymap">
    <area href="<%= Url.RouteUrl("MyRoute", new { param1 = "foo", param2 = 5 }) %>" alt="HTML and CSS Reference" shape="rect" coords="5,5,95,195">
    <area href="<%= Url.RouteUrl("MyRoute", new { param1 = "bar", param2 = 3 }) %>" alt="Design Guide" shape="rect" coords="105,5,195,195">
</map>
<image src="sitemap.gif" alt="Site map" "usemap"="#mymap" width="300" height="200">

Have a look at the different Url.RouteUrl() overloads and/or UrlHelper methods to see which one suits your situation the best.

Once you've sorted that out, my recommendation would be to encapsulate the creation of you area links into a HtmlHelper extension.




回答2:


I was able to substitute the following and it worked just fine:

html example:

<map id='headerMap'>
   <area shape='rect' href="Default.aspx" coords='300,18,673,109' />
</map>

mvc4 example

<map id='headerMap'>
    <area shape="rect" href=@Url.Action("Default", "Home") coords="300,18,673,109">
</map>



回答3:


Another option is to construct your image map with c#. The following link provides some helper methods that creates an image map from code within your view / controller:

http://www.avantprime.com/articles/view-article/9/asp.net-mvc-image-map-helper

Edit: Try http://web.archive.org/web/20110728032820/http://www.avantprime.com/articles/view-article/9/asp.net-mvc-image-map-helper for an archived version of the original link.



来源:https://stackoverflow.com/questions/1355677/asp-net-mvc-how-to-image-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!