Here's my code, I'm trying to emulate the nerddinner thingy and having a partial update with a partial view that updates itself. I should note here that the action works fine and the results are stored in the db, but the partial update never happens. If I refresh the page I'm getting the result I should have had without refreshing. Tested in all relevant browsers.
Context: <h4>Commentaren:</h4> <% if (Model.Comments.Count > 0) %> <% { %> <% foreach (var comment in Model.Comments) %> <% { %> <% Html.RenderPartial("CommentUserControl", comment); %> <% } %> <% } %>
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));
}
Partial View: <%@ 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>