Whether the user has replied You can access the/dinners/details/[ID] URL to view the details of a specific dinner. The implementation of the details action method is not described here. The first step to implement RSVP support is in the dinner object (dinner. CS partial class) to add an isuserregistered (username) auxiliary method, based on whether the user has replied to the dinner, display true or false. Public partial class Dinner { Public bool Isuserregistered ( String Username ){ Return RSVPs. Any (r => r. attendeename. Equals (username, Stringcomparison . Invariantcultureignorecase )); } } Add the following Code Go to the details. aspx view template and display the appropriate information to identify whether the user has registered the dinner or not: <% If(Request. isauthenticated) {%> <% If(Model. isuserregistered (context. User. Identity. Name )) {%> <P> You have registered the dinner!</P> <%} Else {%> <P> You have not registered the dinner yet!</P> <%}%> <%} Else {%> Now<A Href= "/Account/logon">Login</A>. <%}%> Now, when you access a registered dinner, you will see the following information: When you access an unregistered dinner, you will see the following information:
Implement register action
Method The following functions enable users to register banquets on the detail page. To implement this function, we create a new rsvpcontroller class. Implement the register Action Method in the rsvpcontroller class, pass the dinner ID as the parameter, find the corresponding dinner object, and check whether the logged-on user has registered the dinner. If not, add the RSVP object to the database: Public Class Rsvpcontroller:Controller { DinnerrepositoryDinnerrepository =New Dinnerrepository(); // // Ajax:/dinners/rsvpforevent/1 [Authorize,Acceptverbs(Httpverbs. Post)] Public ActionresultRegister (IntID) { DinnerDinner = dinnerrepository. getdinner (ID ); If(! Dinner. isuserregistered (user. Identity. Name )) { RSVPRSVP =New RSVP(); RSVP. attendeename = user. Identity. Name; Dinner. RSVPs. Add (RSVP ); Dinnerrepository. Save (); } ReturnContent (" Thank you.-See you later! " ); } } The above code returns a simple string as the output of the Action method. We can also write this message in the view template. But since it is a simple string, we want the content () helper method of the controller base class to return a string.
Use Ajax
Call register action
Method We will use ajax to call the register Action Method in the detailed page view, which is quite easy to implement. First, we add two reference scripts: <Script SRC= "/Scripts/microsoftajax. js" Type= "Text/JavaScript"> </Script> <Script SRC= "/Scripts/microsoftmvcajax. js" Type= "Text/JavaScript"> </Script> The first database references the core ASP. NET Ajax client script library. This file is about 24 K (Compressed) and contains the core client Ajax function. The second library contains tool functions and is integrated with the Ajax auxiliary methods built in ASP. net mvc. Next, we will update the previously created view template code. Don't just output "You haven't registered this dinner !", Instead, a link is generated. When you click this link, execute the Ajax call and call the register Action Method in rsvpcontroller: <Div ID= "Rsvpmsg"> <% If(Request. isauthenticated) {%> <% If(Model. isuserregistered (context. User. Identity. Name )) {%> <P> You have registered the dinner!</P> <%} Else {%> <%=Ajax. actionlink (" Register the banquet" ,"Register","RSVP",New{Id = model. dinnerid },New Ajaxoptions{Updatetargetid ="Rsvpmsg"})%> <%}%> <%} Else {%> Now<A Href= "/Account/logon">Login</A>. <%}%> </Div> The above Code uses Ajax. the auxiliary method of actionlink () is ASP. net MVC built-in, with HTML. the actionlink () auxiliary method is similar, but does not execute a standard navigation, but calls an action method through Ajax. Above, we call the register Action Method of rsvpcontroller and input model. dinnerid as the ID parameter. The final ajaxoptions parameter indicates receiving the content returned by the Action method and updating the <div> element with the ID rsvpmsg on the page. Now, when you access an unregistered dinner record, you will see the "register this dinner" link:
When the user clicks the "register this dinner" link and calls ajax to the RSVP controller's register action method, the updated message is displayed as follows after the method is called:
The network bandwidth and traffic triggered by the preceding Ajax call are very lightweight.