部分视图的Ajax部分更新无法在ASP.NET MVC2中使用

时间:2010-07-15 20:27:23

标签: ajax asp.net-mvc-2 partial-views

我在尝试部分更新以便在ASP.NET MVC2中工作时遇到了一些麻烦。 (我认为)我遵循我在网上发现的教程非常接近,但Ajax部分不起作用。控制器执行它应该做的没有错误,但页面不会更新自己。当我刷新页面时,我可以看到我的行动结果。

以下是应该自行更新的用户控件的代码:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Ideas.Models.Comment>" %> <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script> <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script> <script type="text/javascript">

    function AnimateVotebox() {
        $("#commentbox").animate({ fontSize: "1.5em" }, 400);
    }

</script> <div id="commentbox">
    <div class="display-label">
        <i><%: Html.ActionLink(Model.User1.UserName, "Details", "User", new { id = Model.User1.LoweredUserName.Replace(' ', '-') }, null)%> zegt:</i>
    </div>
    <div class="display-label"><%:Model.text %></div>
        <% bool canPost = Ideas.Helpers.UserHelper.CanPost(HttpContext.Current); %>
        <% if (Model.CommentVotes.Count != 0) %>
        <% { %>
            <div class="display-label"><%= Html.Encode(Model.UpVotes)%> van de 
                <%= Html.Encode(Model.Votes)%> gaan akkoord.</div>
                <% if (canPost)
                   { %>
                <% if (Model.HasVoted((Guid)Membership.GetUser(Context.User.Identity.Name).ProviderUserKey) < 0) %>
                <% { %>Stem:
                    <%= Ajax.ActionLink("-", "VoteComment", "Votes",
                        new { id = Model.id, up = false },
                        new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%>
                    <%= Ajax.ActionLink("+", "VoteComment", "Votes",
                        new { id = Model.id, up = true },
                        new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%>
                <% } %>
                <% else %>
                <% { %>Wijzig stem:
                    <% if (Model.HasVoted((Guid)Membership.GetUser(Context.User.Identity.Name).ProviderUserKey)
== 0) %>
                    <% { %>
                        <%= Ajax.ActionLink("-", "ChangeCommentVote", "Votes",
                            new { id = Model.id, up = false },
                            new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%>
                        <p style="color:gray; font-size:20;"">+</p>
                    <% } %>
                    <% else %>
                    <% { %>
                        <p style="color:gray; font-size:20;"">-</p>
                        <%= Ajax.ActionLink("+", "ChangeCommentVote", "Votes",
                            new { id = Model.id, up = true },
                            new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%>
                    <% } %>
                <% } %>

                <% } %>
            <br />
        <% } %>
        <% else  %>
        <% { %>
            <div class="display-label">Nog geen stemmen</div><br />
            <% if (canPost)
                   { %>

                    Stem: <%= Ajax.ActionLink("-", "VoteComment", "Votes",
                        new { id = Model.id, up = false },
                        new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%>
                    <%= Ajax.ActionLink("+", "VoteComment", "Votes",
                        new { id = Model.id, up = true },
                        new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%>
                <% } %>
        <% } %>
        <% if (HttpContext.Current.User.IsInRole("Moderator") || HttpContext.Current.User.IsInRole("Administrator"))%>
        <% { %>
            <%: Html.ActionLink("Geef probatie...", "ProbateUser", "Mod", new { comment = Model.id }, null) %>
            <%: Html.ActionLink("Verwijder...", "BanUser", "Mod", new { comment = Model.id }, null) %>
        <% } %>

        </div>

请注意,如果我不使用jQuery,也会出现问题。

这是控制器:

        [UserAuthorize]
    [Authorize]
    public ActionResult VoteComment(int id, bool up)
    {
        Comment comment = crep.GetComment(id);
        CommentVote vote = new CommentVote();
        vote.isup = up;
        vote.user = (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey;
        comment.CommentVotes.Add(vote);
        crep.Save();
        return PartialView("CommentUserControl", crep.GetComment(id));
    }

    [UserAuthorize]
    [Authorize]
    public ActionResult ChangeCommentVote(int id, bool up)
    {
        Comment comment = crep.GetComment(id);
        CommentVote vote = comment.CommentVotes
            .Where(v => v.user == (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey
            && v.comment == id).SingleOrDefault();
        vote.isup = up;
        crep.Save();
        return PartialView("CommentUserControl", crep.GetComment(id));
    }

不知道这是否重要,但是usercontrol被加载到一个视图中,该视图链接到与上面不同的控制器。 ActionLinks工作正常,并在VotesController上触发操作。

2 个答案:

答案 0 :(得分:0)

我没有看到你的ajax调用,但是从你的第一个代码块下面的小片段我假设你正在使用jQuery。我看到IE的问题显示了ajax回调的变化,直到我添加了“cache:false”标志。我想如果这不是问题,它会在所有浏览器中发生吗?也许还要显示你的ajax电话。

答案 1 :(得分:0)

要调用部分更新并调用控制器操作,我使用jQuery,就像这样

 $.getJSON(urlModeli, null, function (data) {
/*Do something with the data*/
}

然后更新内容。

我确实有IE显示新内容的问题,因为我有缓存:在包含我正在更新的数据的选项卡上为true,因此浏览器只缓存旧值并且没有显示新值,除了刷新