Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax Partial Update with Partial View not working in ASP.NET MVC2
    primarykey
    data
    text
    <p>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.</p> <p>Here is the code for the user control that's supposed to update itself:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Ideas.Models.Comment&gt;" %&gt; &lt;script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function AnimateVotebox() { $("#commentbox").animate({ fontSize: "1.5em" }, 400); } &lt;/script&gt; &lt;div id="commentbox"&gt; &lt;div class="display-label"&gt; &lt;i&gt;&lt;%: Html.ActionLink(Model.User1.UserName, "Details", "User", new { id = Model.User1.LoweredUserName.Replace(' ', '-') }, null)%&gt; zegt:&lt;/i&gt; &lt;/div&gt; &lt;div class="display-label"&gt;&lt;%:Model.text %&gt;&lt;/div&gt; &lt;% bool canPost = Ideas.Helpers.UserHelper.CanPost(HttpContext.Current); %&gt; &lt;% if (Model.CommentVotes.Count != 0) %&gt; &lt;% { %&gt; &lt;div class="display-label"&gt;&lt;%= Html.Encode(Model.UpVotes)%&gt; van de &lt;%= Html.Encode(Model.Votes)%&gt; gaan akkoord.&lt;/div&gt; &lt;% if (canPost) { %&gt; &lt;% if (Model.HasVoted((Guid)Membership.GetUser(Context.User.Identity.Name).ProviderUserKey) &lt; 0) %&gt; &lt;% { %&gt;Stem: &lt;%= Ajax.ActionLink("-", "VoteComment", "Votes", new { id = Model.id, up = false }, new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%&gt; &lt;%= Ajax.ActionLink("+", "VoteComment", "Votes", new { id = Model.id, up = true }, new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%&gt; &lt;% } %&gt; &lt;% else %&gt; &lt;% { %&gt;Wijzig stem: &lt;% if (Model.HasVoted((Guid)Membership.GetUser(Context.User.Identity.Name).ProviderUserKey) == 0) %&gt; &lt;% { %&gt; &lt;%= Ajax.ActionLink("-", "ChangeCommentVote", "Votes", new { id = Model.id, up = false }, new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%&gt; &lt;p style="color:gray; font-size:20;""&gt;+&lt;/p&gt; &lt;% } %&gt; &lt;% else %&gt; &lt;% { %&gt; &lt;p style="color:gray; font-size:20;""&gt;-&lt;/p&gt; &lt;%= Ajax.ActionLink("+", "ChangeCommentVote", "Votes", new { id = Model.id, up = true }, new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%&gt; &lt;% } %&gt; &lt;% } %&gt; &lt;% } %&gt; &lt;br /&gt; &lt;% } %&gt; &lt;% else %&gt; &lt;% { %&gt; &lt;div class="display-label"&gt;Nog geen stemmen&lt;/div&gt;&lt;br /&gt; &lt;% if (canPost) { %&gt; Stem: &lt;%= Ajax.ActionLink("-", "VoteComment", "Votes", new { id = Model.id, up = false }, new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%&gt; &lt;%= Ajax.ActionLink("+", "VoteComment", "Votes", new { id = Model.id, up = true }, new AjaxOptions { UpdateTargetId = "commentbox", OnSuccess = "AnimateVotebox" }, null)%&gt; &lt;% } %&gt; &lt;% } %&gt; &lt;% if (HttpContext.Current.User.IsInRole("Moderator") || HttpContext.Current.User.IsInRole("Administrator"))%&gt; &lt;% { %&gt; &lt;%: Html.ActionLink("Geef probatie...", "ProbateUser", "Mod", new { comment = Model.id }, null) %&gt; &lt;%: Html.ActionLink("Verwijder...", "BanUser", "Mod", new { comment = Model.id }, null) %&gt; &lt;% } %&gt; &lt;/div&gt; </code></pre> <p>Note that if I don't use jQuery the problem occurs as well.</p> <p>And here's the controller:</p> <pre><code> [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 =&gt; v.user == (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey &amp;&amp; v.comment == id).SingleOrDefault(); vote.isup = up; crep.Save(); return PartialView("CommentUserControl", crep.GetComment(id)); } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload