简体   繁体   English

从Ajax ActionLink MVC3.0向Javascript metod传递参数

[英]Pass argument to Javascript metod from Ajax ActionLink MVC3.0

I have a problem sending parameter (Id) to javascript function. 我有一个问题发送参数(Id)到JavaScript函数。 Let me tell you about the underlying idea: I have a Training model and every training model has 1 Survey. 让我告诉你一个基本想法:我有一个训练模型,每个训练模型都有一个调查。 What i do in below code is to generate a pop-up link for each Training that employee takes and display a pop-up window. 我在下面的代码中做的是为员工拍摄的每个培训生成一个弹出式链接并显示一个弹出窗口。 For this I used the Ajax ActionLink and in each part to show the popup window I cretead a div with the id of Training. 为此,我使用了Ajax ActionLink,并在每个部分中显示弹出窗口,我发现了一个id为Training的div。 So when onComplete I call a javascript function openPopup to show the popup window but it does not. 因此,当onComplete我调用一个javascript函数openPopup来显示弹出窗口但它没有。 So in conclusion I have different div's in which I will popup a window and I want to pass the id of div to the javascript function. 所以最后我有不同的div,我将弹出一个窗口,我想将div的id传递给javascript函数。 Can you tell me how to solve this problem? 你能告诉我如何解决这个问题吗?

Thank you. 谢谢。

  <script type="text/javascript">
        $(document).ready(function (id) {
            $(id).dialog({
                autoOpen: false,
                title: 'Anket',
                width: 500,
                height: 'auto',
                modal: true
            });
        });
        function openPopup(id) {
            $(id).dialog("open");
        }
    </script>

   foreach (var training in Model.TrainingList)
   {
      <tr>
        <td class="view_detail_label">
                        Eğitim Adı
         </td>
          <td>

          @Ajax.ActionLink(training.Name.Name,
                           "AddSurvey", 
          new { employeeId = Model.Id, trainingId = training.Id },
           new AjaxOptions
             {
            UpdateTargetId = training.Id.ToString(),
              HttpMethod = "GET",
              InsertionMode = InsertionMode.Replace,
               LoadingElementId = "context",
               OnComplete = "openPopup('"+training.Id.ToString()+"')"
                 })

               <div id="@training.Id" style="display:none;"/>  

                    </td>                        
                </tr>                 
               }           
            }
        </table>

You could use a plain Html.ActionLink that you could unobtrusively AJAXify which gives you far more flexibility. 你可以使用一个简单的Html.ActionLink ,你可以不引人注意地使用AJAXify,它可以提供更大的灵活性。 Also you seem to have used some id argument of your document.ready function which doesn't exist and has nowhere to come from. 此外,您似乎已经使用了document.ready函数的一些id参数,该函数不存在且无处可来。 The document.ready callback doesn't take any arguments. document.ready回调不带任何参数。

So: 所以:

<script type="text/javascript">
    $(document).ready(function () {
        $('.addSurvey').click(function() {
            $.ajax({
                url: this.href,
                type: 'GET',
                cache: false,
                context: this,
                success: function(result) {
                    $(this).next('.result').html(result).dialog({
                        autoOpen: true,
                        title: 'Anket',
                        width: 500,
                        height: 'auto',
                        modal: true
                    });
                }
            });
            return false;
        });
    });
</script>

<table>
    @foreach (var training in Model.TrainingList)
    {
       <tr>
           <td class="view_detail_label">
               Eğitim Adı
           </td>
           <td>
               @Html.ActionLink(
                   training.Name.Name, 
                   AddSurvey, 
                   new { 
                       employeeId = Model.Id, 
                       trainingId = training.Id 
                   },
                   new {
                       @class = "addSurvey"
                   }
               )
               <div class="result"></div>
           </td>                        
       </tr>                 
    }
</table>

Also I would very strongly recommend you moving your script out in a separate javascript file and not mix your markup with scripts. 此外,我强烈建议您将脚本移出单独的javascript文件,而不是将标记与脚本混合。

Remark: since you are opening the result of the AJAX call in a modal dialog I don't quite see the point of having a result div in each row. 备注:既然你在模态对话框中打开了AJAX调用的结果,我不太明白在每一行中都有一个结果div。 You could simply move it out from the <table> and have a single one. 你可以简单地从<table>移出它并只有一个。

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

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