如何在MVC视图中调用自动点击事件?

时间:2013-03-07 06:31:29

标签: javascript asp.net-mvc-3 razor asp.net-mvc-views

我有MVC应用程序。 有一个视图包含一些属性和注释链接。 当用户点击评论链接时,评论div得到扩展,所有评论都会显示给用户。 现在我想自动执行该功能,或者我可以说想要跳过该点击过程,当用户看到该视图时应显示所有注释。 (不点击链接,应显示所有评论)

怎么做?

查看代码

            @model PaymentAdviceEntity.Employee

            @{
                ViewBag.Title = "Edit";
            }

            <h2>Edit</h2>

            <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
            <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

            @using (Html.BeginForm()) {
                @Html.ValidationSummary(true)
                   <div class="row-fluid">

                <fieldset>


                    @Html.HiddenFor(model => model.Id)



                    <div class="editor-label span3">
                        @Html.LabelFor(model => model.FirstName,"First Name")
                    </div>
                    <div class="editor-field span3 InputBoxMargin">
                        @Html.EditorFor(model => model.FirstName)
                        @Html.ValidationMessageFor(model => model.FirstName)
                    </div>


                          <div class="editor-label span3">
                          @Html.LabelFor(model => model.LastName,"Last Name")
                          </div>
                          <div class="editor-field span3 InputBoxMargin">
                          @Html.EditorFor(model => model.LastName)
                          @Html.ValidationMessageFor(model => model.LastName)
                          </div>
                       </div>
               </div>
            </div>
                     </fieldset>
            </div>

                     <div>
                      <p>
                        <input type="submit" value="Save" />
                    </p>
                 </div>

<div>


     <span>@Ajax.ActionLink("Comments", null, null, null, new { id = Model.Id, @class = "addremark" })</span>

        <div class="RemarkBox"></div>
        <span class="CommentAdd"></span>

</div>

            }



    $(document).ready(function () {
    //$('.RemarkBox').hide();
    $('a.addremark').click(function (event) {
    ar url = "@Html.Raw(Url.Action("ShowCommentBox", "Comment", new { Id = "idValue", EntityType = "Employee" }))";
     url = url.replace("idValue", event.target.id);
     $('.RemarkBox').load(url);
     $(this).closest('div').find('div.RemarkBox').slideToggle();
     return false;
     });  
     }); 
       </script>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情,主要部分是您需要在文档中调用click函数。

$(document).ready(function () {
    $('a.addremark').click(addremarkClick);

    // Call on load also
    addremarkClick();
});

function addremarkClick(event) {
    var url = '@Html.Raw(Url.Action("ShowCommentBox", "Comment", new { Id = "idValue", EntityType = "Employee" }))';

    if (typeof event !== 'undefined') url = url.replace("idValue", event.target.id);
    else url = url.replace("idValue", $('a.addremark')[0].id);

    $('.RemarkBox').load(url);
    $(this).closest('div').find('div.RemarkBox').slideToggle();
    return false;
}