Sweet merciful Chaos, what did I just write?


  • FoxDev

    The odd syntax is because this is JavaScript with some lovely C#/Razor thrown in.


    So I had to write this:

    var dispensationsAction = '@Url.Action("DispensationsAjax", "PayrollGroup", new { payrollGroupId = @Model.Id, readOnly = @Model.HasActivePayrollBatch }, null)';
    
    //It's a hack, but it's required, otherwise the readOnly flag isn't passed correctly
    dispensationsAction = dispensationsAction.replace('&', '&');
    

    Why? Well, Url.Action auto-escapes ampersands when creating the URL. While this is fine for <a> tags, it doesn't work then JavaScripting your AJAX.


    I don't like writing code that makes my quills twitch; is there another way?



  • @RaceProUK said:

    is there another way?

    Can you show where dispensationsAction is actually used....it would be helpful in possibly locating alternatives...


  • FoxDev

    It goes into a JS method that adds a few more items to the query string, then invokes $.ajax().

    Really, the issue lies with Url.Action().



  • wrap it in @Html.Raw()

    eg

    var dispensationsAction = '@Html.Raw(Url.Action("DispensationsAjax", "PayrollGroup", new { payrollGroupId = @Model.Id, readOnly = @Model.HasActivePayrollBatch }, null))';


  • FoxDev

    Sweet!


Log in to reply