开发者

Ajax Partial Update with Partial View not working in ASP.NET MVC2

开发者 https://www.devze.com 2023-01-07 18:07 出处:网络
I\'ve ran into some trouble trying to get partial updates to work in ASP.NET MVC2. (I think) I followed the tutorials I found online pretty closely, but the Ajax part isn\'t working. The controller do

I've ran into some trouble trying to get partial updates to work in ASP.NET MVC2. (I think) I followed the tutorials I found online pretty closely, but the Ajax part isn't working. The controller does what it's supposed to do without errors, but the page doesn't update itself. When I refresh the page I can see the result of my action though.

Here is the code for the user control that's supposed to update itself:

<%@ 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-lab开发者_如何学JAVAel">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>

Note that if I don't use jQuery the problem occurs as well.

And here's the controller:

        [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));
    }

Don't know if this matters, but the usercontrol is loaded inside a view that's linked to a different controller than the one above. The ActionLinks work fine though and trigger actions on the VotesController.


I don't see your ajax call, but from the little snippet under your first code block I assume you are using jQuery. I was seeing problems with IE showing changes from an ajax callback until I added the "cache: false" flag. I guess if this isn't the problem, does it happen in all browsers? Maybe show your ajax call also.


To call partial update and invoke controller action i use jQuery like this

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

and update the content afterwards.

And i did have a problem with IE showing the new content, cause i had cache:true on tabs that contained the data i was updating so browser just cached the old value and didn't show the new one, except on refresh

0

精彩评论

暂无评论...
验证码 换一张
取 消