简体   繁体   中英

ajax forms and results in popup window

I have a form in DoComment.ascx :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DT.KazBilet.Objects.PublicationComment>" %>
<div class="wrap">
    <h4>Comment</h4>
    <%using (Ajax.BeginForm("DoComment", "Publication", new {id = Model.Publication.OID, parentId = Model.OID},new AjaxOptions()))
      {%>    
    <%=Html.TextAreaFor(x=>x.Text) %>    
    <%-- <textarea style="width: 100%; height: 152px;"></textarea>--%>
    <input type="submit" value="Publish" class="btn ok_btn" />
    <%}%>
</div>

This is my controller's action:

public JsonResult DoComment(PublicationComment model, int id, int parentId)
        {
            PublicationRepository.SaveComment(User.Identity.Name,id, parentId, model.Text);

            return Json(new {
                 Message = "You comment on moderation"
                 });
        }

I want that user clicks on Publish button then show popup window where will be written text from Message .
Can you help me(some code)?

Thanks.

You could subscribe to the OnSuccess javascript event in the AJAX options and then show the JSON result you have retrieved the way you like (new window, div, ...):

<% using (Ajax.BeginForm(
    "DoComment", 
    "Publication", 
    new { id = Model.Publication.OID, parentId = Model.OID },
    new AjaxOptions { OnSuccess = "onSuccess" })
) %>

and then you would define the onSuccess javascript function. Depending on whether you use jQuery or MicrosoftAjax the implementation of this function might slightly vary and more specifically the way to retrieve the JSON result.

For example if you are using MicrosoftAjax (obsolete now):

var onSuccess = function(e) {
    var json = e.get_response().get_object();    
    alert(json.Message);
};

and if you are jQuery:

var onSuccess = function(json) {
    alert(json.Message);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM