p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional ASP.NET MVC 2 (http://p2p.wrox.com/forumdisplay.php?f=588)
-   -   Calling the "Regsiter" action method using AJAX (page 122) (http://p2p.wrox.com/showthread.php?t=81952)

lagonave December 17th, 2010 03:49 PM

Calling the "Regsiter" action method using AJAX (page 122)
 
Hello all,
For some reason, this piece of code does not work at least with me. Here is the code..
--
<%:Ajax.ActionLink("RSVP for this event", "Register","RSVP", new { id=Model.DinnerID }, new AjaxOptions { UpdateTargetId="rsvpmsg"})%>
--

Here is the error that I got: Microsoft JScript runtime error: 'Sys' is undefined.

and the dynamic code shown is ": <a href="/RSVP/Register/9" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'rsvpmsg' });">RSVP for this event</a>

Do you guys have any idea why this mess happen? [:(!]

~L.G

flyinhawaiian December 17th, 2010 04:11 PM

Compare your code with this:

Code:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Models.Dinner>" %>

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>   

<script type="text/javascript">

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

</script>
   
<div id="rsvpmsg">

<% if (Request.IsAuthenticated) { %>

    <% if (Model.IsHostedBy(Context.User.Identity.Name)) { %>

        <p>You are the host for this event!</p>

    <% } else if (Model.IsUserRegistered(Context.User.Identity.Name)) { %>       
   
        <p>You are registered for this event!</p>
   
    <% }
      else
      { %> 
   
        <%: Ajax.ActionLink("RSVP for this event",
                            "Register", "RSVP",
                            new { id = Model.DinnerID },
                            new AjaxOptions { UpdateTargetId = "rsvpmsg", OnSuccess = "AnimateRSVPMessage" })%>       
    <% } %>
   
<% } %>
   
</div>


lagonave December 17th, 2010 04:24 PM

Calling "Register" method using AJAX (page 122)
 
flyinhawaiian,

The only difference that I see from your code is the "AnimateRSVPMessage()" function that you added the code and passed it to the new Ajaxoptions as a parameter.

I don't think that adding that extra function will solve the error that I have gotten. Any other options [:confused:]?

Thank you!

L.G

flyinhawaiian December 17th, 2010 05:12 PM

Check Web.config see if you have:

[code]
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
[\code]

lagonave December 17th, 2010 05:15 PM

Calling the "Register" method using AJAX
 
flyinhawaiian,

I just saw that the code that you have posted is the next step on that tutorial on page 124. I'm still trying to get page 122-123 to work according to the book. Something is missing and I can't put my hands on it yet.

Here is my complete code for the "Detail.aspx":

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Model s.Dinner>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
Details
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2><%: Model.Title %></h2>
<p>
<strong>When:</strong>
<%: Model.EventDate.ToShortDateString() %>
<strong>@</strong>
<%: Model.EventDate.ToShortTimeString() %>
</p>
<p>
<strong>Where:</strong>
<%: Model.Address %>,
<%: Model.Country %>
</p>
<p>
<strong>Description:</strong>
<%: Model.Description %>
</p>
<p>
<strong>Organizer:</strong>
<%: Model.HostedBy %> (<%: Model.ContactPhone %>)
</p>

<div id="rsvpmsg">
<% if(Request.IsAuthenticated){ %>

<% if(Model.IsUserRegistered(Context.User.Identity.Na me)) { %>
<p>You are registered for this event!</p>

<% } else { %>

<%:Ajax.ActionLink("RSVP for this event (SecondTest)", "Register","RSVP", new { id=Model.DinnerID }, new AjaxOptions { UpdateTargetId="rsvpmsg"}) %>

<% } %>

<%} else { %>
<%: Html.ActionLink("Logon to RSVP for this event.", "LogOn", "Account") %>
<% } %>
</div>

<% if (Model.IsHostedBy(Context.User.Identity.Name)) { %>
<%: Html.ActionLink("Edit Dinner", "Edit", new { id = Model.DinnerID })%> |
<%: Html.ActionLink("Delete Dinner", "Delete", new { id = Model.DinnerID })%>
<%} %>

</asp:Content>

lagonave December 17th, 2010 05:31 PM

Yes, I checked the web.config file and it has everything except for the "
Code:

...
part.

flyinhawaiian December 17th, 2010 05:39 PM

Ok one more thing to check. In my notes I put reference to ajax script in Site.Master (this is not in the book).

Try putting this between the <body> </bod> tags.
[code]
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
[\code\

flyinhawaiian December 17th, 2010 05:45 PM

Also, scripts are cached. May need to clear cache in IE (Ctrl+F5). I use fiddler to clear my cache. http://www.fiddler2.com/fiddler2/

lagonave December 17th, 2010 05:56 PM

Calling the "Register" method using AJAX
 
flyinhawaiian,
It works! I have removed the " <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>


and placed them in the Site.Master as suggested. Now it works like a champ. Thank you sir for your help. I appreciate it. [:D]

~L. G.

flyinhawaiian December 17th, 2010 06:08 PM

Two heads are better than one. I knew we could both figure this out. I find that I learn more by helping others. Merry Christmas. :)

caof2005 February 27th, 2011 05:11 AM

Ajax Scripts also works inside <head></head> tags!
 
Quote:

Originally Posted by flyinhawaiian (Post 266691)
Ok one more thing to check. In my notes I put reference to ajax script in Site.Master (this is not in the book).

Try putting this between the <body> </bod> tags.
[code]
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
[\code\

It also works if you put the previous 2 script references within the <head> and </head> tags inside the Site.Master file!!! :)

BrennanFuchs April 6th, 2011 08:40 PM

Still need help with this one!
 
Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /RSVP/Register/9


----------------------
-I've moved the Ajax.js script references to site.master as suggested
-checked config for required namespaces
-Not sure what's missing here as even error occurs before it even hops into the RSVPController. Well not totally true it creates the dinnerRepository
Code:

DinnerRepository dinnerRepository = new DinnerRepository();
and then doesn't seem to recognize the Register function.
-I've also out of curiosity removed the HttpPost it THEN recognized the function but returned the content just to it's own page as opposed to the Details page within the rsvpmsg div...


--------------------------------
Code:

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NerdDinner.Models;

namespace NerdDinner.Controllers
{
    public class RSVPController : Controller
    {

        DinnerRepository dinnerRepository = new DinnerRepository();

        //
        // AJAX: /Dinners/Register/1

        [Authorize, HttpPost]
        public ActionResult Register(int id)
        {

            Dinner dinner = dinnerRepository.GetDinner(id);

            if (!dinner.IsUserRegistered(User.Identity.Name))
            {
                RSVP rsvp = new RSVP();
                rsvp.AttendeeName = User.Identity.Name;

                dinner.RSVPs.Add(rsvp);
                dinnerRepository.Save();
            }

            return Content("Thanks - we'll see you there!");
        }
    }
}


----------------------

Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.Dinner>" %>
 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Dinner: <%: Model.Title %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


<h2><%: Model.Title %></h2>
    <p>
        <strong>When:</strong>
        <%: Model.EventDate.ToShortDateString() %>
       
        <strong>@</strong>
       
        <%: Model.EventDate.ToShortTimeString() %>
    </p>

    <p>
        <strong>Where:</strong>
        <%: Model.Address %>,
          <%: Model.Country%>
    </p>
    <p>
        <strong>Description:</strong>
        <%: Model.Description%>
    </p>
 
    <p>
        <strong>Organizer:</strong>
        <%: Model.HostedBy%>
        (<%: Model.ContactPhone%>)
    </p>

  <div id="rsvpmsg">
    <% if (Request.IsAuthenticated)
      { %>

        <% if (Model.IsUserRegistered(User.Identity.Name))
          { %>       
   
            <p>You are registered for this event!</p>
   
        <% }
          else
          { %> 
   
                <%: Ajax.ActionLink("RSVP for this event",
                        "Register",
                        "RSVP",
                        new { id = Model.DinnerID },
                        new AjaxOptions { UpdateTargetId = "rsvpmsg" })%>
       
        <% } %>
    <% }
      else
      { %>
            <a href="/Account/Logon">Logon</a> to RSVP for this event.
    <% } %>
    </div>

   
    <% if (Model.IsHostedBy(User.Identity.Name))
      { %>
    <%: Html.ActionLink("Edit Dinner", "Edit", new { id = Model.DinnerID })%> |
    <%: Html.ActionLink("Delete Dinner", "Delete", new { id = Model.DinnerID }) %> |
    <% }%>
    <%: Html.ActionLink("Back to List", "Index") %>

</asp:Content>


saymamohib26 April 7th, 2011 03:31 AM

PS3 Hacks
 
I wrote a review of
PS3 Hacks
xbox hacks
psp hacks

saymamohib28 April 7th, 2011 03:32 AM

I wrote a review of about>>>>>>>
erp definiton
erp definiton

flyinhawaiian April 7th, 2011 10:25 AM

BrennanFuchs, when you got the "Server Error in '/' Application." message were you logged in (as a nerd dinner user)? Are you hitting that URL through the javacript or did you manually type that URL in the browser?

BrennanFuchs April 7th, 2011 07:35 PM

yes am logged in as user. One not registered to that dinner of course.
I'm simply clicking the 'RSVP to this event' Ajax.Action link URL on the Details page. So not manually typing.

Thanks so much for the help.. this transition is not a smooth one for this dinosaur..

BrennanFuchs April 14th, 2011 07:30 PM

Issue persists
 
Whats strange is it I can get it to work but just returning the
return Content("Thanks - we'll see you there!");

text to a blank screen if i remove the HttpPost

[Authorize]

as opposed to
[Authorize, HttpPost]

naturally this is not bringing me back to the view but just to a blank screen with this text.

bahusoid April 15th, 2011 11:50 AM

Seems like you are using unobtrusive java script (check in your web.config: <add key="UnobtrusiveJavaScriptEnabled" value="true" />). To fix it add to your View following script:
Code:

<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
Probably you should also specify HttpMethod explicitly:
Code:

                <%: Ajax.ActionLink("RSVP for this event",
                        "Register",
                        "RSVP",
                        new { id = Model.DinnerID },
                        new AjaxOptions { UpdateTargetId = "rsvpmsg", HttpMethod = "Post" })%>


flyinhawaiian April 15th, 2011 12:29 PM

Still not sure what is causing your problem. I don't recall anyone else having same problem. I would suggest reviewing all steps from the book to see if you missed something somewhere (get lastest .net mvc, all windows updates, security updates, etc.)

Any chance you can post your code in a public place? Then I can at least try to reproduce the problem on my machine and compare code.

BrennanFuchs April 19th, 2011 07:54 PM

Still no luck
 
Thanks bahusoid tried you'r suggestion you were correct about the config key entry (unobtrusive) and I've tried adding the script reference and the explicit casting with no change in behavior.

Something to note: i ONLY get this error when i add the HttpPost qualifier to my 'Register' method in the RSVP Controller. [Authorize, HttpPost]

EVEN with the explicit setting of the httppost as suggest by bahusoid in the actionlink if i remove the qualifier it will save the RSVP but not redirect to the div/ajax action control just to a blank screen with the content string. As SOON as I put that HttpPost back [Authorize, HttpPost] it immediately fails out when trying to hop into the Register method.

Without the post it hops in fine and does the save i just dont post back to the detail page but to RSVP/Register/[id] empty screen...

Flyinhawaiian: I've pulled and recopied every code snippet just to be sure :S. I had MVC3 installed before I even started this book. I appreciate all the effort guys, finally have the work comp setup to a degree when i have the time ill be dragging this in to have a second and 3rd set of eyes on this as well.

As for posting it to a public place i could email you my project? if you want to hit me up kweejibow@hotmail.com

Thanks
Brennan

bahusoid April 20th, 2011 01:03 AM

Try to disable unobtrusive java-script and check. If it works that way then may be unobtrusive library was not referenced correctly.

flyinhawaiian April 20th, 2011 01:42 PM

I think this is your problem.... in your Dinners/Details.aspx you are setting HttpMethod to "Get" when the action is expecting a post.


Code:

      <%: Ajax.ActionLink("RSVP for this event",
                        "Register",
                        "RSVP",
                        new { id = Model.DinnerID },
                        new AjaxOptions { UpdateTargetId = "rsvpmsg", HttpMethod = "Get" })%>
    <% } %>


BrennanFuchs April 20th, 2011 06:42 PM

Nah... I just put that in there to try and see if that would change behavior.

It does it wit post get or nothing as long as post is above the register function.

flyinhawaiian April 21st, 2011 01:00 PM

Well, in this case, HttpMethod should not be set to "Get".

I was able to run your code and post to the Register method. I set a breakpoint inside Register() and code execution did stop there.

noni460 December 12th, 2012 08:37 AM

i have the same problem with the ajax scripts and i put the scripts tags in <body> of the master page nothing appeared to me accept the background color any help plz ????????????


All times are GMT -4. The time now is 05:20 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.