简体   繁体   English

MVC如何将整个Div变成一个动作链接

[英]MVC How to make a whole Div into an action link

I was wondering how you would go about making a div box into an action link. 我想知道您将如何将div框制作为动作链接。 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. 我看到了一些使用Ajax的示例,但它们都指向部分视图,因为我只需要将其作为另一个视图的链接即可。

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

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. 与其使div成为具有onclicks的链接(例如,这可能会导致想要在新标签页中打开您的链接的人遇到麻烦),还不如选择其他方式并使常规操作链接成为块级元素。 You can do this by adding a class to your ActionLink that contains the rules you need. 您可以通过在ActionLink中添加一个包含所需规则的类来做到这一点。 Something like this 像这样

CSS 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"})

(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!) (请原谅我,如果那一点是错误的,我不记得在不打开Visual Studio的情况下html属性会走到哪里!我也是用VB而不是C#编写的,所以不知道html属性的语法是否正确。有人从内存编码方面比我更好请随时纠正!)

What you end up with in HTML HTML的最终结果

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

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. 这意味着您不需要周围的容器,不需要任何JavaScript,并且最终仍带有block元素。 The buttons at the top of StackOverflow are good examples of this. StackOverflow顶部的按钮就是很好的例子。 Of course, this is only valid if you were only putting text in the div. 当然,这仅在您仅将文本放在div中时才有效。 If you intended to have other tags/things within your div then this would not be an appropriate solution. 如果您打算在div中包含其他标签/事物,那么这将不是一个合适的解决方案。

You could make a custom HTML helper to do that. 您可以创建一个自定义HTML助手来做到这一点。 Something along these lines: 遵循以下原则:

 public static MvcHtmlString DivActionLink(this HtmlHelper html,string linktext, [AspMvcAction] string action,
            [AspMvcController] string controller, object routeValues, IDictionary<string, Object> htmlAttributes, bool useNewWindow = false)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);

            var routeDictionary = new RouteValueDictionary(routeValues);

            var windowLocation = useNewWindow 
                ? string.Format("window.open('{0}', 'new window');", url.Action(action, controller, routeDictionary))
                : string.Format("window.location='{0}';", url.Action(action, controller, routeDictionary)) ;

            var divTag = new TagBuilder("div");
            divTag.Attributes.Add("onclick", windowLocation);
            divTag.Attributes.Add("style", "cursor: pointer;");
            divTag.InnerHtml = linktext;

            return MvcHtmlString.Create(divTag.ToString(TagRenderMode.Normal));
        }

And then use it like so 然后像这样使用

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

and optionally set the last parameter to true to have the link open in a new window 并可以选择将最后一个参数设置为true以在新窗口中打开链接

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

EDIT 编辑

The [AspMvcController] and [AspMvcAction] attributes are in a Jetbrains namespace and are a part of Resharper. [AspMvcController][AspMvcAction]属性位于Jetbrains命名空间中,并且是Resharper的一部分。 You can remove them from the method above and it will still work the same. 您可以从上述方法中将其删除,并且仍然可以正常工作。

You wouldn't need ajax since you are just trying to redirect to a new site. 您不需要ajax,因为您只是尝试重定向到新站点。

You don't need an action link. 您不需要操作链接。 Just add an click event handler to your div. 只需在您的div中添加一个click事件处理程序即可。

<div @("onclick=GoToRoom('" + item.id + "')") >
  Room
</div>

And then your javascript looks like this 然后你的JavaScript看起来像这样

function GoToRoom(id){
   window.location = "/Home/room?id=" + id;
}

UPDATE: Changed the code so that it would generate the onclick attribute correctly. 更新:更改了代码,以便它将正确生成onclick属性。 Adding the inline @item.id could cause the html to look funky. 添加内联@ item.id可能导致html看起来很时髦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM