Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

I have jquery function like below:
JavaScript
$(document).ready(function () {

           $(".Tile").click(function () {

               //$(this).submit();
               $.ajax({
                   url: "@(Url.Action("Create", "Task"))",
                   type: 'POST',
                   success: function () {
                       alert("success");
                   },
                   error: function () {
                       alert('error');
                   }
               });
           });
       });




HTML
<div class="row">
                       <div class="col-xs-5">
                           <div class="Tile" id="FQR"><h1 class="text-center">FQR</h1></div>
                       </div>
                       <div class="col-xs-5">
                           <div class="Tile"><h1 class="text-center">MQR</h1> </div>
                       </div>
                   </div>




Onclick of
Tile It should navigate to create controller but its not working
could you please help
Posted
Updated 15-Jan-15 19:40pm
v2
Comments
[no name] 16-Jan-15 0:55am    
Hi - is it hitting Create Method in Task Controller. If yes you can debug and find what is going wrong.
Vivek S Kale 16-Jan-15 6:37am    
yes it is hitting create action

1 solution

This is because of the conflict of class.
Classes can be applied to n number of html elements to maintain some commoness in css. But, for a click event it is always wise to use Identifiers(id).

So in the div tags apart from class add separate id to each and then go like below:-


HTML
<div class="row">
                       <div class="col-xs-5">
                           <div class="Tile" id="FQR"><h1 class="text-center">FQR</h1></div>
                       </div>
                       <div class="col-xs-5">
                           <div class="Tile" id="MQR"><h1 class="text-center">MQR</h1> </div>
                       </div>
                   </div>


You can't use '@Url.Action("SomeAction", "SomeController")' in js file, because this is ASP.NET MVC helper, if you put your code to the view everything will work.
You can hard code the url also. Like below:-

JavaScript
$(document).ready(function () {
 
            $("#MQR/FQR").click(function () {
                $.ajax({
                    url: "/Create/Task",
                    type: 'POST',
                    success: function () {
                        alert("success");
                    },
                    error: function () {
                        alert('error');
                    }
                });
            });
        });

Please post back your queries if any.
Thanks
 
Share this answer
 
Comments
Vivek S Kale 16-Jan-15 6:46am    
it navigating to Create Action, I have create action as below:
public ActionResult Create()
{

return View("Create");
}


but it is not navigating to Create View..
I have tried to use this.returnView also
[no name] 16-Jan-15 7:21am    
I did not understand. Please brief again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900