Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telerik MVC ScriptRegistrar duplicating javascript

I am attempting to register some javascript in my view, and I seem to be having an issue. In my master page, at the bottom I have:

@(
    Html.Telerik().ScriptRegistrar()
        .OnDocumentReady(@<text>
            // Open the hidden window when the feedback-link is clicked
            $('#feedback-link').click(function(e) {
                e.preventDefault();
                $('#FeedbackWindow').data('tWindow').center().open();
            });
        </text>)
)

In my view, I want some view specific javascript, so I have:

@(Html.Telerik().ScriptRegistrar().OnDocumentReady(
    @<text>
        // Upon contact selection change, update the contact sidebar summary
        $('#contactlist').change(function() {
            alert('Selected id' + $(this).val());
        });
    </text>)
)

Unfortunately, this is causing my view's javascript from being declared both in the MVC view, and in my master page when the final page is rendered. How can I get this to only register the script once?

like image 897
KallDrexx Avatar asked Jul 29 '26 07:07

KallDrexx


1 Answers

As I said in my forum reply the ScriptRegistrar is being output twice because told so. The @() Razor expression will output its contents whereas @{ } will be executed. In your case you need to use @{ } for the specific script:

@{ Html.Telerik().ScriptRegistrar().OnDocumentReady(
    @<text>
        // Upon contact selection change, update the contact sidebar summary
        $('#contactlist').change(function() {
            alert('Selected id' + $(this).val());
        });
    </text>);
}

Also note that the @{ } block requires a semi-colon.

like image 161
Atanas Korchev Avatar answered Jul 31 '26 20:07

Atanas Korchev