MVC如何将整个Div变成一个动作链接 [英] MVC How to make a whole Div into an action link

查看:110
本文介绍了MVC如何将整个Div变成一个动作链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您将如何将div框制作为动作链接.我看到了一些使用Ajax的示例,但它们都指向部分视图,因为我只需要将其作为另一个视图的链接即可.

I was wondering how you would go about making a div box into an action link. I seen some examples using Ajax but they were directing towards partial view where as I just need it to be a link to another view.

 <div id="container">
     @Html.ActionLink("Room", "room", "Home", new { id = item.Id }, null)
 </div>

推荐答案

您可以随时访问另一种方法是使您的常规操作链接到块级元素.您可以通过在ActionLink中添加一个包含所需规则的类来做到这一点.像这样

Rather than make the div a link with onclicks (which can cause problems for people who want to open your link in a new tab for example) you could always go the other way and make your regular action link a block level element. You can do this by adding a class to your ActionLink that contains the rules you need. Something like this

CSS

.blockLink{
    display:block;
    padding:10px;
    background-color:pink; /*maybe not pink, but you get the idea*/
}

MVC ActionLink

MVC ActionLink

@Html.ActionLink("Room", "room", "Home", new { id = item.Id }, new{class = "blockLink"})

(请原谅我,如果那一点是错误的,我不记得在没有打开Visual Studio的情况下html属性会去哪里!我也是用VB而不是C#编写的,所以不知道html属性的语法是否正确.有人比我强可以随时从内存中进行编码!)

(Forgive me if that bit is wrong, I never remember where the html attributes go without Visual Studio open! I also write in VB not C#, so not idea if the syntax of the htmlattributes is right. Someone better than me at coding from memory feel free to correct it!)

您最终用HTML编写的内容

What you end up with in HTML

<a class="blockLink" href="/room/1">Room</a>

这意味着您不需要周围的容器,不需要任何JavaScript,并且最终仍带有block元素. StackOverflow顶部的按钮就是很好的例子.当然,仅当您仅在div中放置文本时,这才有效.如果您打算在div中包含其他标签/事物,那么这将不是一个合适的解决方案.

This would mean you don't need a surrounding container, you don't need any JavaScript and you still end up with a block element. The buttons at the top of StackOverflow are good examples of this. Of course, this is only valid if you were only putting text in the div. If you intended to have other tags/things within your div then this would not be an appropriate solution.

这篇关于MVC如何将整个Div变成一个动作链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆