4

On the server-side I have a transaction which returns a JsonResult:

public JsonResult DoStuff(Guid id, string userInputText)
{
     var product = _repository.Product(id); //busines logic

     //Only a specific product must have userInputText <= 10 characters. 
     //Other products may have as many characters as the user wants.
     if(product == Enum.SpecificProduct && userInputText.Count() > 10)
     {
          //The user input text comes from the View...
          //If it has more then 10 characters, need to send the errorMessage to the View.
          return Json(new { success = false, errorMessage = "error message" }, JsonRequestBehavior.AllowGet);
     }

     //Otherwise, do stuff on the product... 

     //and return success at the end.
     return Json(new { success = true });
}

On the other hand, on the client-side I have this:

using (Ajax.BeginForm("DoStuff", ajaxOptions))
{
    <span>Enter the text:</span>
    @Html.TextArea("userInputText", new { onkeyup = "SyncContents(); return false;" })

    <input type="submit" value="Add" />
    <!-- error message should be displayed here-->
}

This is the AjaxOptions:

var ajaxOptions= new AjaxOptions
{
    OnSuccess = "reload",
    OnFailure = "FailMessage"
};

If the entered text have more then 10 characters, when the "Add" button is pressed, the Controller is being executing the code on the server-side and fails, how can I get the errorMessage from there and use it here, in the View, to inform the user ?

I tried to alert a message:

<script>
    function FailMessage() {
        alert("Fail Post");
    }
</script>

But no pop-up "Fail post" appears.

Best regards.

9
  • I am not sure how your ajaxOptions is connected with saveOptions in the code above. maybe change your ajaxOptions to new Ajaxoptions { OnSuccess = "reload", OnFailure = "FailMessage" }, and remember to add a "," after "reload" Commented Sep 13, 2017 at 16:36
  • Really sorry for those mistakes, I've just edited my question. saveOptions is ajaxOption actually, I was coping the wrong name. Thank you. Commented Sep 13, 2017 at 16:41
  • So, the user can only type in up to 10 letters in the text-area.. if the user types in 11 or greater characters.. then you want an error message? Commented Sep 13, 2017 at 17:02
  • @T_Roy yes, that's exactly what I need. Commented Sep 13, 2017 at 17:05
  • Is there a reason as to why you are checking that on the server side? That can be done with client-side validation Commented Sep 13, 2017 at 17:05

1 Answer 1

2

The problem here is the Ajax helper thinks all your responses are successful. Your controller action is returning HTTP 200 so there isn't a problem.

https://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxoptions.onfailure(v=vs.118).aspx#P:System.Web.Mvc.Ajax.AjaxOptions.OnFailure

AjaxOptions.OnFailure Property

This function is called if the response status is not in the 200 range.

So you'll need to use the success handler and explicitly check the JSON success parameter.

Or have your action change the HttpStatusCode for the response.

if (notValid)
{
    Response.StatusCode = 400;  // Bad Request
    return Json(new { success = false, errorMessage = "error message" }, JsonRequestBehavior.AllowGet);
}

But for a validation error here I'd just check the for an error in the success handler.

And yes, you should validate on the client and on the server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.