ASP.NET MVC - How to call controller method and don't leave the view [duplicate]










1
















This question already has an answer here:



  • ASP.NET MVC - How to call void controller method without leaving the view?

    7 answers



I have looked around for a similar question, but all the answers don't work for my problem. I am creating a wishlist, and I want the link to only return some sort of confirmation, and not go to another view.



View:



...
<div class="wishlist">
@Html.ActionLink(" ", "AddToWishList", "Wishlist", routeValues: new

itm_id = item.id
,
htmlAttributes: new @class = "heart" )
</div>
...


Controller



 public async Task<ActionResult> AddToWishList(int itm_id, int recordId)

...
return RedirectToAction(nameof(Index));










share|improve this question















marked as duplicate by Stijn, BWA, user3559349 Nov 14 '18 at 20:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • @Stijn I looked at that question, but that doesn't resolve my problem. When I implement the question, it returns me to an empty page.

    – floorvmt
    Nov 14 '18 at 15:07






  • 1





    Are you sure you've correctly implemented it? I haven't verified the code from that answer but it looks correct at first sight.

    – Stijn
    Nov 14 '18 at 15:08











  • Read about Ajax.

    – BWA
    Nov 14 '18 at 15:10











  • Yea this part should stop page refresh from happening $("#myLink").click(function(e){ e.preventDefault(); You would have to add an id to your actionLink and replace the myLink

    – Ryan Schlueter
    Nov 14 '18 at 15:10












  • @Stijn Yes, it doesn't work for me.

    – floorvmt
    Nov 15 '18 at 14:30















1
















This question already has an answer here:



  • ASP.NET MVC - How to call void controller method without leaving the view?

    7 answers



I have looked around for a similar question, but all the answers don't work for my problem. I am creating a wishlist, and I want the link to only return some sort of confirmation, and not go to another view.



View:



...
<div class="wishlist">
@Html.ActionLink(" ", "AddToWishList", "Wishlist", routeValues: new

itm_id = item.id
,
htmlAttributes: new @class = "heart" )
</div>
...


Controller



 public async Task<ActionResult> AddToWishList(int itm_id, int recordId)

...
return RedirectToAction(nameof(Index));










share|improve this question















marked as duplicate by Stijn, BWA, user3559349 Nov 14 '18 at 20:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • @Stijn I looked at that question, but that doesn't resolve my problem. When I implement the question, it returns me to an empty page.

    – floorvmt
    Nov 14 '18 at 15:07






  • 1





    Are you sure you've correctly implemented it? I haven't verified the code from that answer but it looks correct at first sight.

    – Stijn
    Nov 14 '18 at 15:08











  • Read about Ajax.

    – BWA
    Nov 14 '18 at 15:10











  • Yea this part should stop page refresh from happening $("#myLink").click(function(e){ e.preventDefault(); You would have to add an id to your actionLink and replace the myLink

    – Ryan Schlueter
    Nov 14 '18 at 15:10












  • @Stijn Yes, it doesn't work for me.

    – floorvmt
    Nov 15 '18 at 14:30













1












1








1


1







This question already has an answer here:



  • ASP.NET MVC - How to call void controller method without leaving the view?

    7 answers



I have looked around for a similar question, but all the answers don't work for my problem. I am creating a wishlist, and I want the link to only return some sort of confirmation, and not go to another view.



View:



...
<div class="wishlist">
@Html.ActionLink(" ", "AddToWishList", "Wishlist", routeValues: new

itm_id = item.id
,
htmlAttributes: new @class = "heart" )
</div>
...


Controller



 public async Task<ActionResult> AddToWishList(int itm_id, int recordId)

...
return RedirectToAction(nameof(Index));










share|improve this question

















This question already has an answer here:



  • ASP.NET MVC - How to call void controller method without leaving the view?

    7 answers



I have looked around for a similar question, but all the answers don't work for my problem. I am creating a wishlist, and I want the link to only return some sort of confirmation, and not go to another view.



View:



...
<div class="wishlist">
@Html.ActionLink(" ", "AddToWishList", "Wishlist", routeValues: new

itm_id = item.id
,
htmlAttributes: new @class = "heart" )
</div>
...


Controller



 public async Task<ActionResult> AddToWishList(int itm_id, int recordId)

...
return RedirectToAction(nameof(Index));





This question already has an answer here:



  • ASP.NET MVC - How to call void controller method without leaving the view?

    7 answers







c# html asp.net asp.net-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 16:11







floorvmt

















asked Nov 14 '18 at 15:03









floorvmtfloorvmt

829




829




marked as duplicate by Stijn, BWA, user3559349 Nov 14 '18 at 20:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Stijn, BWA, user3559349 Nov 14 '18 at 20:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • @Stijn I looked at that question, but that doesn't resolve my problem. When I implement the question, it returns me to an empty page.

    – floorvmt
    Nov 14 '18 at 15:07






  • 1





    Are you sure you've correctly implemented it? I haven't verified the code from that answer but it looks correct at first sight.

    – Stijn
    Nov 14 '18 at 15:08











  • Read about Ajax.

    – BWA
    Nov 14 '18 at 15:10











  • Yea this part should stop page refresh from happening $("#myLink").click(function(e){ e.preventDefault(); You would have to add an id to your actionLink and replace the myLink

    – Ryan Schlueter
    Nov 14 '18 at 15:10












  • @Stijn Yes, it doesn't work for me.

    – floorvmt
    Nov 15 '18 at 14:30

















  • @Stijn I looked at that question, but that doesn't resolve my problem. When I implement the question, it returns me to an empty page.

    – floorvmt
    Nov 14 '18 at 15:07






  • 1





    Are you sure you've correctly implemented it? I haven't verified the code from that answer but it looks correct at first sight.

    – Stijn
    Nov 14 '18 at 15:08











  • Read about Ajax.

    – BWA
    Nov 14 '18 at 15:10











  • Yea this part should stop page refresh from happening $("#myLink").click(function(e){ e.preventDefault(); You would have to add an id to your actionLink and replace the myLink

    – Ryan Schlueter
    Nov 14 '18 at 15:10












  • @Stijn Yes, it doesn't work for me.

    – floorvmt
    Nov 15 '18 at 14:30
















@Stijn I looked at that question, but that doesn't resolve my problem. When I implement the question, it returns me to an empty page.

– floorvmt
Nov 14 '18 at 15:07





@Stijn I looked at that question, but that doesn't resolve my problem. When I implement the question, it returns me to an empty page.

– floorvmt
Nov 14 '18 at 15:07




1




1





Are you sure you've correctly implemented it? I haven't verified the code from that answer but it looks correct at first sight.

– Stijn
Nov 14 '18 at 15:08





Are you sure you've correctly implemented it? I haven't verified the code from that answer but it looks correct at first sight.

– Stijn
Nov 14 '18 at 15:08













Read about Ajax.

– BWA
Nov 14 '18 at 15:10





Read about Ajax.

– BWA
Nov 14 '18 at 15:10













Yea this part should stop page refresh from happening $("#myLink").click(function(e){ e.preventDefault(); You would have to add an id to your actionLink and replace the myLink

– Ryan Schlueter
Nov 14 '18 at 15:10






Yea this part should stop page refresh from happening $("#myLink").click(function(e){ e.preventDefault(); You would have to add an id to your actionLink and replace the myLink

– Ryan Schlueter
Nov 14 '18 at 15:10














@Stijn Yes, it doesn't work for me.

– floorvmt
Nov 15 '18 at 14:30





@Stijn Yes, it doesn't work for me.

– floorvmt
Nov 15 '18 at 14:30












2 Answers
2






active

oldest

votes


















2














You want to use Ajax.ActionLink instead of Html.ActionLink. The article below should specify some of the details of the usage. Make sure you remember to include the jquery libraries or it will not generate the link type you are expecting.



https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/



Personally, I would not use the Microsoft method and write a jQuery Ajax postback myself in JavaScript.






share|improve this answer























  • This doesn't work for me unfortunately, it doesn't recognize Ajax

    – floorvmt
    Nov 15 '18 at 14:29


















1














You can achieve this simply using Jquery Get. https://api.jquery.com/jquery.get/



This is your back end side.



public async Task<JsonResult> AddToWishList(int itm_id, int recordId)

// Do you data here and put it to the List<something> list
return Json(list, JsonRequestBehavior.AllowGet);



and your front end should be something like this.



<button>Click</button>

<div class="wishlist">
</div>

<script>
$("button").click(function()
$.get( "YourControllerName/AddToWishList",itm_id: yourItemId, recordId: yourRecordId function( data )
$( ".wishlist" ).html( data );
alert( "Load was performed." );
);
);
</script>





share|improve this answer























  • This doesn't work for me unfortunately, it doesn't recognize JsonRequestBehavior.AllowGet

    – floorvmt
    Nov 15 '18 at 14:29











  • How do you mean? JsonRequestBehavior is an enum under System.Web.Mvc imgur.com/a/b6xK3Rm

    – Mehmet Taha Meral
    Nov 15 '18 at 15:40












  • I only get these options when typing Json.. imgur.com/a/bmZ4V66

    – floorvmt
    Nov 18 '18 at 15:36











  • I tried to do it this way: return Json(itm_id); but it still doesn't work for me..

    – floorvmt
    Nov 18 '18 at 15:44

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You want to use Ajax.ActionLink instead of Html.ActionLink. The article below should specify some of the details of the usage. Make sure you remember to include the jquery libraries or it will not generate the link type you are expecting.



https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/



Personally, I would not use the Microsoft method and write a jQuery Ajax postback myself in JavaScript.






share|improve this answer























  • This doesn't work for me unfortunately, it doesn't recognize Ajax

    – floorvmt
    Nov 15 '18 at 14:29















2














You want to use Ajax.ActionLink instead of Html.ActionLink. The article below should specify some of the details of the usage. Make sure you remember to include the jquery libraries or it will not generate the link type you are expecting.



https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/



Personally, I would not use the Microsoft method and write a jQuery Ajax postback myself in JavaScript.






share|improve this answer























  • This doesn't work for me unfortunately, it doesn't recognize Ajax

    – floorvmt
    Nov 15 '18 at 14:29













2












2








2







You want to use Ajax.ActionLink instead of Html.ActionLink. The article below should specify some of the details of the usage. Make sure you remember to include the jquery libraries or it will not generate the link type you are expecting.



https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/



Personally, I would not use the Microsoft method and write a jQuery Ajax postback myself in JavaScript.






share|improve this answer













You want to use Ajax.ActionLink instead of Html.ActionLink. The article below should specify some of the details of the usage. Make sure you remember to include the jquery libraries or it will not generate the link type you are expecting.



https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/



Personally, I would not use the Microsoft method and write a jQuery Ajax postback myself in JavaScript.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 15:38









Brandon BarkleyBrandon Barkley

426312




426312












  • This doesn't work for me unfortunately, it doesn't recognize Ajax

    – floorvmt
    Nov 15 '18 at 14:29

















  • This doesn't work for me unfortunately, it doesn't recognize Ajax

    – floorvmt
    Nov 15 '18 at 14:29
















This doesn't work for me unfortunately, it doesn't recognize Ajax

– floorvmt
Nov 15 '18 at 14:29





This doesn't work for me unfortunately, it doesn't recognize Ajax

– floorvmt
Nov 15 '18 at 14:29













1














You can achieve this simply using Jquery Get. https://api.jquery.com/jquery.get/



This is your back end side.



public async Task<JsonResult> AddToWishList(int itm_id, int recordId)

// Do you data here and put it to the List<something> list
return Json(list, JsonRequestBehavior.AllowGet);



and your front end should be something like this.



<button>Click</button>

<div class="wishlist">
</div>

<script>
$("button").click(function()
$.get( "YourControllerName/AddToWishList",itm_id: yourItemId, recordId: yourRecordId function( data )
$( ".wishlist" ).html( data );
alert( "Load was performed." );
);
);
</script>





share|improve this answer























  • This doesn't work for me unfortunately, it doesn't recognize JsonRequestBehavior.AllowGet

    – floorvmt
    Nov 15 '18 at 14:29











  • How do you mean? JsonRequestBehavior is an enum under System.Web.Mvc imgur.com/a/b6xK3Rm

    – Mehmet Taha Meral
    Nov 15 '18 at 15:40












  • I only get these options when typing Json.. imgur.com/a/bmZ4V66

    – floorvmt
    Nov 18 '18 at 15:36











  • I tried to do it this way: return Json(itm_id); but it still doesn't work for me..

    – floorvmt
    Nov 18 '18 at 15:44















1














You can achieve this simply using Jquery Get. https://api.jquery.com/jquery.get/



This is your back end side.



public async Task<JsonResult> AddToWishList(int itm_id, int recordId)

// Do you data here and put it to the List<something> list
return Json(list, JsonRequestBehavior.AllowGet);



and your front end should be something like this.



<button>Click</button>

<div class="wishlist">
</div>

<script>
$("button").click(function()
$.get( "YourControllerName/AddToWishList",itm_id: yourItemId, recordId: yourRecordId function( data )
$( ".wishlist" ).html( data );
alert( "Load was performed." );
);
);
</script>





share|improve this answer























  • This doesn't work for me unfortunately, it doesn't recognize JsonRequestBehavior.AllowGet

    – floorvmt
    Nov 15 '18 at 14:29











  • How do you mean? JsonRequestBehavior is an enum under System.Web.Mvc imgur.com/a/b6xK3Rm

    – Mehmet Taha Meral
    Nov 15 '18 at 15:40












  • I only get these options when typing Json.. imgur.com/a/bmZ4V66

    – floorvmt
    Nov 18 '18 at 15:36











  • I tried to do it this way: return Json(itm_id); but it still doesn't work for me..

    – floorvmt
    Nov 18 '18 at 15:44













1












1








1







You can achieve this simply using Jquery Get. https://api.jquery.com/jquery.get/



This is your back end side.



public async Task<JsonResult> AddToWishList(int itm_id, int recordId)

// Do you data here and put it to the List<something> list
return Json(list, JsonRequestBehavior.AllowGet);



and your front end should be something like this.



<button>Click</button>

<div class="wishlist">
</div>

<script>
$("button").click(function()
$.get( "YourControllerName/AddToWishList",itm_id: yourItemId, recordId: yourRecordId function( data )
$( ".wishlist" ).html( data );
alert( "Load was performed." );
);
);
</script>





share|improve this answer













You can achieve this simply using Jquery Get. https://api.jquery.com/jquery.get/



This is your back end side.



public async Task<JsonResult> AddToWishList(int itm_id, int recordId)

// Do you data here and put it to the List<something> list
return Json(list, JsonRequestBehavior.AllowGet);



and your front end should be something like this.



<button>Click</button>

<div class="wishlist">
</div>

<script>
$("button").click(function()
$.get( "YourControllerName/AddToWishList",itm_id: yourItemId, recordId: yourRecordId function( data )
$( ".wishlist" ).html( data );
alert( "Load was performed." );
);
);
</script>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 16:09









Mehmet Taha MeralMehmet Taha Meral

569416




569416












  • This doesn't work for me unfortunately, it doesn't recognize JsonRequestBehavior.AllowGet

    – floorvmt
    Nov 15 '18 at 14:29











  • How do you mean? JsonRequestBehavior is an enum under System.Web.Mvc imgur.com/a/b6xK3Rm

    – Mehmet Taha Meral
    Nov 15 '18 at 15:40












  • I only get these options when typing Json.. imgur.com/a/bmZ4V66

    – floorvmt
    Nov 18 '18 at 15:36











  • I tried to do it this way: return Json(itm_id); but it still doesn't work for me..

    – floorvmt
    Nov 18 '18 at 15:44

















  • This doesn't work for me unfortunately, it doesn't recognize JsonRequestBehavior.AllowGet

    – floorvmt
    Nov 15 '18 at 14:29











  • How do you mean? JsonRequestBehavior is an enum under System.Web.Mvc imgur.com/a/b6xK3Rm

    – Mehmet Taha Meral
    Nov 15 '18 at 15:40












  • I only get these options when typing Json.. imgur.com/a/bmZ4V66

    – floorvmt
    Nov 18 '18 at 15:36











  • I tried to do it this way: return Json(itm_id); but it still doesn't work for me..

    – floorvmt
    Nov 18 '18 at 15:44
















This doesn't work for me unfortunately, it doesn't recognize JsonRequestBehavior.AllowGet

– floorvmt
Nov 15 '18 at 14:29





This doesn't work for me unfortunately, it doesn't recognize JsonRequestBehavior.AllowGet

– floorvmt
Nov 15 '18 at 14:29













How do you mean? JsonRequestBehavior is an enum under System.Web.Mvc imgur.com/a/b6xK3Rm

– Mehmet Taha Meral
Nov 15 '18 at 15:40






How do you mean? JsonRequestBehavior is an enum under System.Web.Mvc imgur.com/a/b6xK3Rm

– Mehmet Taha Meral
Nov 15 '18 at 15:40














I only get these options when typing Json.. imgur.com/a/bmZ4V66

– floorvmt
Nov 18 '18 at 15:36





I only get these options when typing Json.. imgur.com/a/bmZ4V66

– floorvmt
Nov 18 '18 at 15:36













I tried to do it this way: return Json(itm_id); but it still doesn't work for me..

– floorvmt
Nov 18 '18 at 15:44





I tried to do it this way: return Json(itm_id); but it still doesn't work for me..

– floorvmt
Nov 18 '18 at 15:44



這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto