本文介绍了在MVC 5 Razor视图中调用JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一篇文章中看到你可以在你的剃刀代码中调用一个JavaScript函数,如下所示:

I have seen in another post that you can call a JavaScript function in your razor code like so:

@:FunctionName()

对我来说虽然这只输出实际的单词 FunctionName()

For me though this only outputs the actual words FunctionName()

以下是我的观点:

@model PriceCompare.Models.QuoteModel

@{
    ViewBag.Title = "Quote";
}

<h2>Quote</h2>

@if (@Model.clarify == true)
{
    // do drop down loic
    @:ShowClarify();
}
else
{
    // fill quote
    @:ShowQuote();
}
<div class="clarify">

    You can see the clarify div
</div>
<div class="quote">

    You can see the quote div
</div>

@section head {

    <script type="text/javascript">

        $(document).ready(
            function ShowQuote() {
                $(".quote").show();
            },
            function ShowClarify() {
                $(".clarify").show();
            }
        );

    </script>
}

是不是因为我把它嵌套在`@if'中?无论如何?

Is it because I have nested it in an `@if'? Anyway around this?

推荐答案

你需要把你的javascript放在< script> 标签,你需要调用其范围内的函数:

You need to put your javascript in a <script> tag, and you need to call the functions within their scope:

<script type="text/javascript">

    $(document).ready(
        function ShowQuote() {
            $(".quote").show();
        },
        function ShowClarify() {
            $(".clarify").show();
        }

        @if (@Model.clarify == true)
        {
            // do drop down loic
            ShowClarify();
        }
        else
        {
            // fill quote
            ShowQuote();
        }
    );

</script>

这篇关于在MVC 5 Razor视图中调用JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:36