1
Vote

Razor and JavaScript parsing issue

description

Created a new MVC4 web application using the mobile template. I added a controller based on a EF entity which resulted in some code being generated and a view which has JavaScript representing the client API at the end. The JavaScript contains a method "deleteRow" which gets rendered incorrectly by the Razor view engine.
 
The problem seems to be that the Razor view engine does not recognise the inserted commands (e.g. "'@Html.Raw(Url.Action("Delete", new { id = "__ID__" }))'") under certain conditions. I have attached the full view file.
 
The method is as follow:
 
    function deleteRow(row) {
        if (confirm('Are you sure you want to delete this row?')) {
            $.post('@Html.Raw(Url.Action("Delete", new { id = "__ID__" }))'.replace(/__ID__/, row.data('pkey')))
                .done(function() {
                    row.remove();
                    totalRowCount--;
                })
                .fail(function() {
                    alert('Row deletion failed.');
                });
        }
    }
 
Changing the method to the following results in the view being rendered correctly:
 
    function deleteRow(row) {
        if (confirm('Are you sure you want to delete this row?')) {
            var url = '@Html.Raw(Url.Action("Delete", new { id = "__ID__" }))';
            $.post(url.replace(/__ID__/, row.data('pkey')))
                .done(function() {
                    row.remove();
                    totalRowCount--;
                })
                .fail(function() {
                    alert('Row deletion failed.');
                });
        }
    }

file attachments

comments

schalkvanwyk wrote May 8, 2012 at 8:02 PM

Please ignore, coincidently posted to wrong project