简体   繁体   English

如果Controller失败,如何在View中显示错误? ASP.NET MVC

[英]How to show error in View if the Controller fails ? ASP.NET MVC

On the server-side I have a transaction which returns a JsonResult : 服务器端,我有一个返回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: 这是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 ? 如果输入的文本超过10个字符,当按下“添加”按钮时,控制器正在服务器端执行代码并失败,如何从那里获取errorMessage并在此处使用它,在视图中,告知用户?

I tried to alert a message: 我试着提醒一条消息:

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

But no pop-up "Fail post" appears. 但是没有弹出“失败的帖子”出现。

Best regards. 最好的祝福。

The problem here is the Ajax helper thinks all your responses are successful. 这里的问题是Ajax帮助器认为你的所有响应都是成功的。 Your controller action is returning HTTP 200 so there isn't a problem. 您的控制器操作正在返回HTTP 200,因此没有问题。

https://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxoptions.onfailure(v=vs.118).aspx#P:System.Web.Mvc.Ajax.AjaxOptions.OnFailure 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 AjaxOptions.OnFailure属性

This function is called if the response status is not in the 200 range. 如果响应状态不在200范围内,则调用此函数。

So you'll need to use the success handler and explicitly check the JSON success parameter. 因此,您需要使用成功处理程序并显式检查JSON success参数。

Or have your action change the HttpStatusCode for the response. 或者让您的操作更改响应的HttpStatusCode。

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. 是的,您应该在客户端和服务器上进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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