0

I am working with MVC4 Razor pages, in page I have one code block for jquery. In my script file I have one function like

 fucntion MyAjaxRequest(ClickedElement, RequestType, RequestUrl, PostParentForm, PostformID,RequestData,ReturnFunction){}

from this function I send ajax request to call controller method. To specify which controller and controller method to be call I am passing value in RequestUrl parameter like

@section JSSection{
<script type="text/javascript">
    $('.star-icon').click(function () {

        MyAjaxRequest(this, 'get', 'Url.Action("MethodNameOfController", "ControllerName")', null, null, { EmailID: $(this).attr('value') }, 
     function (result, clickedElement) {

         if (result.ResponseData == "False") {

             $(clickedElement).removeClass("star-icon stared").addClass("star-icon unstared");

         }
         else if (result.ResponseData == "True") {

             $(clickedElement).removeClass("star-icon unstared").addClass("star-icon stared");

         }
         else {

         }

     });
    });

</script>
}

but in this case its not working properly because I need to add @ before Url.Action(.... like

   @section JSSection{
<script type="text/javascript">
    $('.star-icon').click(function () {

        MyAjaxRequest(this, 'get', '@Url.Action("MethodNameOfController", "ControllerName")', null, null, { EmailID: $(this).attr('value') }, 
     function (result, clickedElement) {

         if (result.ResponseData == "False") {

             $(clickedElement).removeClass("star-icon stared").addClass("star-icon unstared");

         }
         else if (result.ResponseData == "True") {

             $(clickedElement).removeClass("star-icon unstared").addClass("star-icon stared");

         }
         else {

         }

     });
    });

</script>
}

but problem is that when I use @ in jquery code it will close section early before tag and not consider

);
</script>
}

Can anybody tell me how can I solve this?

2
  • 1
    Try escaping @ like this @@ Commented Feb 15, 2014 at 7:43
  • I tried it also but Escaping also not working. Still its closing block early. Commented Feb 15, 2014 at 9:31

1 Answer 1

1

Try putting the url into a javascript variable and then passing that to your function.

You may be able to debug better afterwards.

$('.star-icon').click(function ()
{
    var url = '@Url.Action("MethodNameOfController", "ControllerName")';

    MyAjaxRequest(this, 'get', url, null, null, { EmailID: $(this).attr('value') }, 
 function (result, clickedElement) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.