I have a Delete ActionLink. When clicked it naturally goes to my Delete Action, deletes the record by the id passed to it's action param. I need to put in a Delete confirmation with JQuery. Not sure how or if you can do this with an ActionLink but it
seems as though JQuery isn't recognizing my ActionLink. Below is what I have setup:
In the View that has my Delete link I have this JQuery script:
<script type="text/javascript">
$('.Delete').click(function(){
var answer = confirm('Confirm deletion of this Page');
return answer
});
</script>
...
and then further down my ActionLink in this page:
<%=Html.ActionLink("Delete", "Delete", new { Controller = "Page", ID = this.ViewData.Page.PageID }) %>
Also, what I don't understand is WHERE to put the class property so that JQuery can pick this anchor tag up once rendered. I mean, for instance my anonymous method I have, I believe I should put something like this:
<%=Html.ActionLink("Delete", "Delete", new { Controller = "Page", ID = this.ViewData.Page.PageID, class = "Delete" }) %>
But then I believe wouldn't it then put "class" in the QueryString instead of rendering it to the <a> tag because I don't have a param in my Controller's action which then MVC will append it to QueryString? I guess I don't know where the line is drawn when
MVC wires up controller ations with anonymous methods and then trying to mix that with the actual tag's rendered properties once all said and done and trying to specify both action params and properties that are to be rendered to the tag itself and how those
can mix together in asp.net mvc.
Firstly you'll want to fire your script once the page has finished building: I think in jQuery, if you pass a function to $ it will fire it after the page load.
$( function() {
$('.Delete').click(function(){
var answer = confirm('Confirm deletion of this Page');
return answer
});
});
Alright thanks. But...so our team talked about this. There is no overload to allow you to add in HTML attributes. There is an overload that lets you put in objects. But we're using that for our routing, for querystring, or what have you. You can't just
put class = "delete" in my existing anonymous method param because it will not interpret it as an HTML attribute when I have other items defined in that anonymous method if you take a look at the example I have.
We'd have to possibly extend the ActionLink to include a new custom param to allow us to spit back HTML attributes if we're already using objects for non-attributes in an HTML helper method. This is a limitation or should I say missing functionality that
is in out of box MVC helper methods right now. No overload exists to have basically 2 object params, one for stuff you want to do that's custom and another right after for HTML specific attributes only in a defined ActionLink.
That's fine if you ONLY want to pass one value. However, there's still the problem that if you want to pass querystring values you can't. Where would you put that? You can't. What I'm saying is ActionLink out of the box is not flexible. Your example
does solve my initial problem so thanks. But, I am also noticing that if I wnat to add any more params later for lets say a querystring or whatever, I won't be able to.
Thanks for your help using the lambda expression. It solved my problem. We're definitely using Preview 2 but we have not seen a way that you claim. If you look at the overloads of ActionLink for example we don't see a way to include both at all.
Take a look at the Url.Action again, I think it does what you need. You can manually control the link yourself and still have your query string values passed over.
<a href="<%=Url.Action("Delete","Delete", new { Key="Value"} ) %>" class="Delete">Delete</a>
It will provide this link http://localhost/Delete/Delete?Key=Value and you will have your confirmation from jQuery. Hope it helps!
dba123
Contributor
2726 Points
1364 Posts
JQuery Confirmation on click of ActionLink
May 19, 2008 01:26 AM|LINK
I have a Delete ActionLink. When clicked it naturally goes to my Delete Action, deletes the record by the id passed to it's action param. I need to put in a Delete confirmation with JQuery. Not sure how or if you can do this with an ActionLink but it seems as though JQuery isn't recognizing my ActionLink. Below is what I have setup:
In the View that has my Delete link I have this JQuery script:
<script type="text/javascript">
$('.Delete').click(function(){
var answer = confirm('Confirm deletion of this Page');
return answer
});
</script>
...
and then further down my ActionLink in this page:
<%=Html.ActionLink("Delete", "Delete", new { Controller = "Page", ID = this.ViewData.Page.PageID }) %>
Also, what I don't understand is WHERE to put the class property so that JQuery can pick this anchor tag up once rendered. I mean, for instance my anonymous method I have, I believe I should put something like this:
<%=Html.ActionLink("Delete", "Delete", new { Controller = "Page", ID = this.ViewData.Page.PageID, class = "Delete" }) %>
But then I believe wouldn't it then put "class" in the QueryString instead of rendering it to the <a> tag because I don't have a param in my Controller's action which then MVC will append it to QueryString? I guess I don't know where the line is drawn when MVC wires up controller ations with anonymous methods and then trying to mix that with the actual tag's rendered properties once all said and done and trying to specify both action params and properties that are to be rendered to the tag itself and how those can mix together in asp.net mvc.
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: JQuery Confirmation on click of ActionLink
May 19, 2008 03:08 AM|LINK
Firstly you'll want to fire your script once the page has finished building: I think in jQuery, if you pass a function to $ it will fire it after the page load.
$( function() {
$('.Delete').click(function(){
var answer = confirm('Confirm deletion of this Page');
return answer
});
});
to set the class use
new { @class = "Delete" }
jhurrell
Member
12 Points
6 Posts
Re: JQuery Confirmation on click of ActionLink
May 19, 2008 02:24 PM|LINK
Alternatively, you could include the delete confirmation in the link itself by coding the onclick:
onclick="javascript:return confirm('Are you sure you want to delete this item?')"
- John
"Religion ends and philosophy begins, just as alchemy ends and chemistry begins and astrology ends, and astronomy begins."
- Christopher Hitchins
dba123
Contributor
2726 Points
1364 Posts
Re: JQuery Confirmation on click of ActionLink
May 19, 2008 02:47 PM|LINK
Alright thanks. But...so our team talked about this. There is no overload to allow you to add in HTML attributes. There is an overload that lets you put in objects. But we're using that for our routing, for querystring, or what have you. You can't just put class = "delete" in my existing anonymous method param because it will not interpret it as an HTML attribute when I have other items defined in that anonymous method if you take a look at the example I have.
We'd have to possibly extend the ActionLink to include a new custom param to allow us to spit back HTML attributes if we're already using objects for non-attributes in an HTML helper method. This is a limitation or should I say missing functionality that is in out of box MVC helper methods right now. No overload exists to have basically 2 object params, one for stuff you want to do that's custom and another right after for HTML specific attributes only in a defined ActionLink.
Jabo4
Member
12 Points
6 Posts
Re: JQuery Confirmation on click of ActionLink
May 19, 2008 08:41 PM|LINK
I would suggest using something like this:
<a href="<%=Url.Action("Delete","Delete") %>" class="Delete">Delete</a>I personally use the lambas but this is very clean and easy to understand where code ends and markup begins :)
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: JQuery Confirmation on click of ActionLink
May 19, 2008 09:02 PM|LINK
I would suggest doing something like this:
<%= Html.ActionLink<PageController>( c => c.Delete(ViewData.Page.PageID), "Delete", new { @class = "delete" } ) %>
DONE!
dba123
Contributor
2726 Points
1364 Posts
Re: JQuery Confirmation on click of ActionLink
May 19, 2008 10:45 PM|LINK
That's fine if you ONLY want to pass one value. However, there's still the problem that if you want to pass querystring values you can't. Where would you put that? You can't. What I'm saying is ActionLink out of the box is not flexible. Your example does solve my initial problem so thanks. But, I am also noticing that if I wnat to add any more params later for lets say a querystring or whatever, I won't be able to.
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: JQuery Confirmation on click of ActionLink
May 19, 2008 11:13 PM|LINK
What version of the Framework are you using?
As of Preview 2 (i think), ActionLink works with multiple url parameters and any extra values are appended to the querystring.
dba123
Contributor
2726 Points
1364 Posts
Re: JQuery Confirmation on click of ActionLink
May 20, 2008 01:58 AM|LINK
Thanks for your help using the lambda expression. It solved my problem. We're definitely using Preview 2 but we have not seen a way that you claim. If you look at the overloads of ActionLink for example we don't see a way to include both at all.
Jabo4
Member
12 Points
6 Posts
Re: JQuery Confirmation on click of ActionLink
May 20, 2008 01:06 PM|LINK
Take a look at the Url.Action again, I think it does what you need. You can manually control the link yourself and still have your query string values passed over.
<a href="<%=Url.Action("Delete","Delete", new { Key="Value"} ) %>" class="Delete">Delete</a>