0

I have a simple javascript function in a partial view:

@section Scripts{
    <script type="text/javascript">

        function showErrors(data) {
            alert('show errors');
        }

    </script>
}

and I need to call this function from my partial view razor code. I've tried something like:

        @if (Model.ErrorMessage.Count > 0)
        {
            var errors = Html.Partial("_errors", Model.ErrorMessage);
            <script type="text/javascript">showErrors(@errors)</script> 
        }

but it's not working. I can't call this function when the page loads because this view will be rendered trough ajax call...

Any ideas on how to modify this code so that the javascript method is called from my partial view when it's loaded?

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • If you load it via ajax, why don't you just call the function via ajax `success` section? – Kamil T Apr 22 '16 at 08:05
  • because in my ajax call I return a partial view and it is rendered and there is no simple way (or I don't know it) to access the model and to check some stuff – Buda Gavril Apr 22 '16 at 08:07

1 Answers1

2

you can not have section in partial view

so try move

<script type="text/javascript">

    function showErrors(data) {
        alert('show errors');
    }

</script>

to your layout or else where and see if it work

Alan Tsai
  • 2,465
  • 1
  • 13
  • 16
  • Thanks, this is the correct answer :-) See also http://stackoverflow.com/questions/7556400/injecting-content-into-specific-sections-from-a-partial-view-asp-net-mvc-3-with –  Apr 22 '16 at 08:52