Raj
Raj

Reputation: 21

I want to code in MVC razor

I want the below functionality in MVC razor.

<asp:Button ID="Button1" runat="server" Text="Delete" onclick="Button1_Click"  OnClientClick="CheckDelete()"/>

I used

@Html.ActionLink("Delete", "Action", "Controller", new { onClientclick = "checkDelete();" }, null)

But this is not working.

Upvotes: 0

Views: 1140

Answers (3)

Karthik M R
Karthik M R

Reputation: 990

HtmlAttributes can be specified in the 5th parameter. So, You need to use the below way:

@Html.ActionLink("Delete", "Action", "Controller", new {id=yourid}, new { onclick = "return checkDelete();" })

Note-You need to use onclick attribute. Since MVC doesn't support server controls, you can't use server side events

Upvotes: 1

Sathish Prabhakaran
Sathish Prabhakaran

Reputation: 261

You cant trigger Action & onclick at a time. So do your code in Action Method using this

 @Url.Action("Action", "Controller", new { Parameter = Parameter })

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129832

OnClientClick is an attribute for runat="server" elements where OnClick is reserved for serverside events.

The ActionLink HTML helper accepts a collection of HTML attributes, and does not have this concern with server OnClick events, so you're supposed to pass onclick (if you don't want to bind the event externally)

Upvotes: 3

Related Questions