Note that there are some explanatory texts on larger screens.

plurals
  1. POController AJAX method to return AJAX ActionLink
    primarykey
    data
    text
    <p>I am fairly new to MVC and just trying to achieve something which I think shouldn't be too complicated to achieve. Just want to know what the best approach for that is. I have an Event-RSVP application (NerdDinner kind) where you go to view details of the event and then click on an AJAX link that will RSVP you for the event.</p> <pre><code> &lt;% if (Model.HasRSVP(Context.User.Identity.Name)) { %&gt; &lt;p&gt; You are registered for this event!&amp;nbsp; &lt;%: Ajax.ActionLink("Click here if you can't make it!", "CancelRegistration", "RSVP", new { id = Model.RSVPs.FirstOrDefault(r =&gt; r.AttendeeName.ToLower() == User.Identity.Name.ToLower()).RSVPID }, new AjaxOptions { UpdateTargetId = "QuickRegister"}) %&gt; &lt;/p&gt; &lt;% } else { %&gt; &lt;p&gt; &lt;%: Ajax.ActionLink("RSVP for this event", "Register", "RSVP", new { id=Model.EventID }, new AjaxOptions { UpdateTargetId="QuickRegister" }) %&gt; &lt;/p&gt; &lt;% } %&gt; </code></pre> <p>Now corresponding to these two links, my functions in RSVP Controller look like these.</p> <pre><code>[Authorize, HttpPost] public ActionResult Register(int id) { Event event = eventRepository.GetEvent(id); if (event == null) return Content("Event not found"); if (!event.IsUserRegistered(User.Identity.Name)) { RSVP rsvp = new RSVP(); rsvp.AttendeeName = User.Identity.Name; event.RSVPs.Add(rsvp); eventRepository.Save(); } return Content("Thanks, you are registered."); } [Authorize, HttpPost] public ActionResult CancelRegistration(int id) { RSVP rsvp = eventRepository.GetRSVP(id); if (rsvp == null) return Content("RSVP not found"); if (rsvp.Event.IsUserRegistered(User.Identity.Name)) { eventRepository.DeleteRSVP(rsvp); eventRepository.Save(); } return Content("Sorry, we won't be seeing you there!"); } </code></pre> <p>Both of these seem to work without any issues. Now I want to make it a little fancier by doing either of these two:</p> <p>1) Return an AJAX link from controller so that when you register, you get cancel registration link shown to you without page refresh.</p> <p>2) Somehow make the view rendering refreshed when the controller method has finished executing so the first code block in my question gets executed after the click of any of the AJAX links. So clicking on register will register you and show you cancel link and clicking on cancel will cancel your registration and show you register link.</p> <p>Any help would be greatly appreciated. Thanks.</p>
    singulars
    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.
 

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