Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

asp.net - How to Use Ajax.BeginForm OnSuccess and OnFailure Methods?

I using this Ajax.BeginForm

    <% using( Ajax.BeginForm( "Create","Mandate",
                       new AjaxOptions( ) {
                           OnSuccess = "GoToMandates",
                           OnFailure = "ShowPopUpError"
                       } ) ) {%>

<% } %>

What do I need to write in the controler to catch this OnSucces and OnFailure.

Because OnSuccess I need to show Success message

OnFailure I need to show othere message.

In my Controller

Public ActionResult GetSomething(FromCollection collection)
{
     if(exists == null)
     {
          //OnSuccess
     }
     else
     { 
         //OnFailure
     }
}

Can anydboy help me out.. how to catch this?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The OnSuccess and OnFailure looks like they are expecting javascript callback functions.

<script type="text/javascript">
    function handleError(ajaxContext) {
    var response = ajaxContext.get_response();
    var statusCode = response.get_statusCode();
    alert("Sorry, the request failed with status code " + statusCode);
    }
</script>

<%= Ajax.ActionLink("Click me", "MyAction",
new AjaxOptions { UpdateTargetId = "myElement", OnFailure = "handleError"}) %>

Example from Pro ASP.NET Framework page 425

ASP.NET AjaxOptions Class


Added Controller Example

The simpliest way to do this would be what I've got here but I recommend looking into strongly typed mvc views using some kind of ViewModel and maybe look into using jQuery for your ajax. With that said this should hopefully work for you.

if (exists)
{
  ViewData["msg"] = "Some Success Message";
}
else
{
  ViewData["msg"] = "Some Error Message";
}

return View();

In your view

<div id="myResults" style="border: 2px dotted red; padding: .5em;">
    <%: ViewData["msg"]%>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...