2

I am using ASP.Net-MVC3 and I use this query in

Layout.cshtml:

$(function() {
    $.ajax({
        url: '@Url.Action("EtiketGetir", "Etiket")',
        type: "POST",
        data: {
            data: data
        },
        success: function(msg) {},
        error: function(msg) {}
    });​

That works in layout.cshtml, but I copied that code and pasted to test.js file and it stopped working.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
altandogan
  • 1,245
  • 6
  • 21
  • 44

3 Answers3

5

That is because only razor handle the@ sign, but it doesn't do it with js files.

url: '@Url.Action("EtiketGetir", "Etiket")',

For solutions read this post:
asp.net-mvc: razor '@' symbol in js file

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
1

What I usually do in these cases is create an initialize function in my javascript file and pass it this data:

// test.js
(function(test, $, undefined) {
    test.initialize = function(options) {
        this.someUrl = options.someUrl;
    }

    test.someFunctionThatUsesUrl = function() {
        $.ajax({
            url: this.someUrl
        });
    }
})(window.test = window.test || {}, jQuery);

Then in the page that is using test.js I will call the initialize function:

<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
    test.initialize({
        someUrl: '@Url.Action("Index", "Home")'
    });
Dismissile
  • 32,564
  • 38
  • 174
  • 263
1

i solved my problem to use this code

$(function() {
    $.ajax({
        url: '/ControllerName/IndexName/',
        type: "POST",
        data: {
            data: data
        },
        success: function(msg) {},
        error: function(msg) {}
    });​
BenMorel
  • 34,448
  • 50
  • 182
  • 322
altandogan
  • 1,245
  • 6
  • 21
  • 44