KallDrexx
KallDrexx

Reputation: 27803

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?

Upvotes: 3

Views: 1717

Answers (1)

Atanas Korchev
Atanas Korchev

Reputation: 30671

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.

Upvotes: 4

Related Questions