简体   繁体   中英

Is it possible to add both a css class AND a return confirm on an Html.ActionLink?

I'm just getting into MVC, so I'm not sure this can even be accomplished using an ActionLink. But what I'm trying to do is to have an Html.ActionLink that you can assign both a CSS class AND a return confirm.

I've gotten this to work:

@Html.ActionLink("Delete", "Delete", "NWS", new { recordId = Model.Id }, new { @class = "btn btn-danger configDelete" })

And I've gotten this to work:

@Html.ActionLink("Delete", "Delete", "NWS", new { recordId = Model.Id }, new { onclick = "return confirm('Are you sure you want to delete this config?')" })

But when I try adding both like so,

@Html.ActionLink("Delete", "Delete", "NWS", new { recordId = Model.Id }, new { @class = "btn btn-danger configDelete" }, new { onclick = "return confirm('Are you sure you want to delete this config?')" })

I get a syntax error that reads, "There is no argument given that corresponds to the required formal parameter 'routeValues' of 'IHtmlHelper.ActionLink(string, string, string, string, string, string, object, object)"

I've tried putting the return confirm before the CSS class too, but it didn't matter. Is this possible to do with an ActionLink? If not, is there something out there where I can do this?

Instead of separating them into their own objects, combine them into one object:

new { @class = "btn btn-danger configDelete", onclick = "return confirm('Are you sure you want to delete this config?')" }

Note that they're both just attributes for the resulting HTML element. You can add as many as you like. They just all need to be a part of the same object passed to the HTML helper method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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