Reprint Address: http://www.cnblogs.com/QLeelulu/archive/2008/10/22/1317214.html
Prior to the ASP.net MVC beta release, m$ announced support for open source JS framework jquery, and then asp.net mvc after the beta release, after you build a asp.net MVC beta project, You can find ASP.net ajax and jquery JS in the project's scripts directory. Anyway, I prefer jquery, so for m$ this move is quite gratifying.
Needless to say, we use AJAX to achieve the function of commenting. Let's take a look at how to use the M$ JS framework for asynchronous AJAX requests.
First of all, of course is to introduce m$ Ajax framework JS: < script src = "/content/microsoftajax.js" type = "Text/javascript" ></script >
< script src = "/content/microsoftmvcajax.js" type = "Text/javascript" ></script >
The helper approach to the framework of ASP.net MVC provides support for his own Ajax, using the method below the System.Web.Mvc.Ajax namespace. You can write code like this:
Or:
There is no EndForm method in Ajaxhelper, you can write HTML directly to close the form, or you can use Html.endform () to close it. OK, let's write a ajaxform for comment:
Here's a detailed ajaxoptions of the various properties of the optional configuration. Public String confirm : No test, do not know what to use, know what to say.
Public string httpmethod : is the HTTP method that specifies the request, "POST" "get" "put" and so on
public &NBSP;INSERTIONMODE&NBSP;INSERTIONMODE&NBSP: The manner in which the returned content is to be updated with the target element. There are three ways:
replace : Replace the contents of the target element;
InsertBefore : The return content is inserted before the target element;
insertafter: The return content is inserted after the target element.
Public string &NBSP;LOADINGELEMENTID&NBSP: Specifies the ID
public string of the loading element for the prompt to display when an asynchronous request is made &NBSP;ONBEGIN&NBSP: JavaScript method triggered before sending an asynchronous request
public string OnComplete : JavaScript method
Public string onfailure that triggers after sending an asynchronous request: A JavaScript method that fires when an asynchronous request fails to be sent
public String onsuccess : A JavaScript method that fires when an asynchronous request is sent
public string UpdateTargetId : Specifies the ID of the target element to be updated for the returned content
Public string url : The URL of the request, and the URL of the action for the form is not specified.
In the above code, a JavaScript method named Clearcomment is invoked after the success of the asynchronous request to clear the comment for the input box, and the return content will replace the contents of the ID boxcomments element. The complete client code is as follows:
<div class= "Entry" >
<%
Html.renderpartial ("_commentlist", ViewData.Model.Comments);
if (BlogSettings.Instance.IsCommentsEnabled) {
Ajax.beginform ("AddComment", new {controller = "Home", id = ""}, New Ajaxoptions ()
{
HttpMethod = "Post",
Loadingelementid = "Loading",
Onbegin = "Commentvalidate",
onsuccess = "Clearcomment",
Updatetargetid = "Boxcomments",
Insertionmode = Insertionmode.replace
}, new {id = "commentform"});
%>
<p> Welcome to leave your comments, your support, is my biggest power!</p>
<p><label for= "Author" >name (required) </label>
<input type= "Text" tabindex= "1" size= "value=" "id=" "Author" name= "Author"/>
<%= html.validationmessage ("Author")%></p>
<p><label for= "Email" >e-mail (won't be published) (required) </label>