简体   繁体   English

ASP.NET MVC5从局部视图调用JavaScript函数

[英]ASP.NET MVC5 Call JavaScript function from Partial View

I want to call JavaScript function, to which I pass parameter, from MVC5 Partial view. 我想从MVC5局部视图调用向其传递参数的JavaScript函数。

I have declared the function addItem(item) in separate JS file at the top of the file, before $(document).ready(function {}) function. 我已经在$(document).ready(function {})函数之前在文件顶部的单独JS文件中声明了addItem(item) $(document).ready(function {})函数。

I have tried the following code: 我尝试了以下代码:

@foreach (string item in (TempData["Items"] as List<string>))
{
    TempData["Item"] = item;
    <script type="text/javascript">addItem(@TempData["Item"]);</script>
}

And the following: 以及以下内容:

@foreach (string item in (TempData["Items"] as List<string>))
{
    <script type="text/javascript">addItem(item);</script>
}

both tries end up with the following error: Uncaught ReferenceError: theValueOfMyItem is not defined . 两种尝试均以以下错误结束: Uncaught ReferenceError: theValueOfMyItem is not defined

I even tried this: 我什至尝试了这个:

@foreach (string item in (TempData["Items"] as List<string>))
{
    addItem(item);
}

but as expected, this gives me error The name 'addItem' does not exist in the current context . 但是,正如所料,这给了我错误The name 'addItem' does not exist in the current context

So my question is: 所以我的问题是:

Can I call JS function with parameter from partial view, and if possible what is the proper way of doing this? 我可以从局部视图中调用带有参数的JS函数吗,如果可能的话,正确的方法是什么?

Edit 1 编辑1

After the suggestions of Carsten Løvbo Andersen and Satpal I get the following error: Uncaught TypeError: addItem is not a function . 根据CarstenLøvboAndersenSatpal的建议,我得到以下错误: Uncaught TypeError: addItem is not a function

Here is how I defined the function: 这是我定义函数的方式:

function addItem(item) {
    // processing...
}

In your Partial View 1st convert Items List into Javascript Array and use javascript for loop to call your addItem method. 在您的Partial View 1st中,将Items List转换为Javascript Array,并使用javascript for循环调用您的addItem方法。

<script type="text/javascript">
    $(function(){
        var itemArray = @Html.Raw(Json.Encode(TempData["Items"] as List<string>));

        for (var item in itemsArray) {
            //call your javascript method with param item
            addItem(item);
        }
    })
</script>

another option with c# foreach loop C# foreach循环的另一个选择

<script type="text/javascript">
        $(function () {
            @foreach (var item in (TempData["Items"] as List<string>))
            {   //your javascript method with param item.
                @:addItem(@item);
            }
        })
</script>

or 要么

<script type="text/javascript">

    $(function () {
        var itemsArray = [];
        @foreach (var item in (TempData["Items"] as List<string>))
        {
            @:itemsArray.push("@item");
        }


        for (var item in itemsArray) {
            //call your javascript method with param item
            addItem(item);
        }
    })


</script>

您需要将引号中的item传递为字符串,否则将被视为变量,从而导致错误。

<script>addItem("@item");</script>

In your Layout page define a section: 在布局页面中,定义一个部分:

@RenderSection("Scripts", false)

and now you can render your code from your Partial directly to the section 现在,您可以将您的代码从Partial直接呈现到该部分

<div>Some DIV</div>
@section Scripts
{
    <script>
        @foreach (string item in (TempData["Items"] as List<string>))
        {
            addItem('@item');
        }
    </script>
}
<div>Some DIV</div>
<!-- You can mix it up. Everything outside of the @section will be treated as normal markup. -->

Perhaps you mind reading this article http://www.codeguru.com/columns/dotnet/using-sections-and-partials-to-manage-razor-views.htm 也许您介意阅读这篇文章http://www.codeguru.com/columns/dotnet/using-sections-and-partials-to-manage-razor-views.htm

What you can do is, put your data in a global variable 您可以做的是,将数据放在全局变量中

 `<script>
    var items = @(TempData["Items"] as List<string>); 
    foreach (var item in items)
    {
        addItem(item);
    }
  </script>`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM