在ASP.Net MVC核心返回错误请求微软不显眼的jQuery阿贾克斯后有ModelState中未

2019-09-27 05:46发布

问题

我试图返回从ModelState中使用错误请求(ModelState中)服务端验证错误,但是当我尝试从指定的xhr.responseText了jQuery的ModelState我在javascript越来越不确定的错误。 我使用ASP.Net核心1.0.1与3.2.3 Microsoft.jQuery.Unobtrusive.Ajax。

编码

控制器动作

public async Task<IActionResult> ApplySanction(SanctionViewModel model)
{
    try
    {
        if (ModelState.IsValid)
        {
            //update database
            return RedirectToAction("Details");
        }
        else
        {
            return BadRequest(ModelState);
        }
    }
    catch (Exception ex)
    {
        return StatusCode(500, ex.ToString());
    }
}

查看(JavaScript的)

function AjaxOnFailure(xhr, status, error) {
    console.log(xhr.responseText);

    var errText = "";
    var response = null;
    var errors = [];
    var errorsString = "";
    if (xhr.status == 400) {
        try {
            response = JSON.parse(xhr.responseText);
            console.log(response);
        }
        catch (e) {
            errText = "Error: " + xhr.status + " - " + error;
        }
    }
    if (response != null) {
        var modelState = response.ModelState; //This is returning undefined
        console.log(modelState);
        for (var key in modelState) {
            if (modelState.hasOwnProperty(key)) {
                for (var i = 0; i < modelState[key].length; i++) {
                    errorsString = (errorsString == "" ? "" : errorsString + "<br/>") + modelState[key][i];
                    errors.push(modelState[key][i]);
                }
            }
        }
    }
    if (errorsString != "") { 
        errText = "Validation Error: " + errorsString;
    } 

    console.log(errText);
    var errIcon = "fa fa-exclamation-circle"
    document.getElementById("modalErrorIcon").className = errIcon;
    document.getElementById("modalErrorText").innerHTML = errText;
}

查看(表)

<form asp-controller="Client" asp-action="ApplySanction" class="form-horizontal" data-ajax="true" data-ajax-method="POST" data-ajax-success="CloseModal(xhr, status, '#sanctionModal')" data-ajax-failure="AjaxOnFailure(xhr, status, error)">
    <div class="modal fade" id="sanctionModal" tabindex="-1" role="dialog" aria-labelledby="sanctionModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="sanctionModalLabel">Sanction Reason</h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" asp-for="ID" />
                    <div class="form-group">
                        <div class="col-sm-10">
                            <textarea asp-for="SanctionReason" class="form-control" autofocus></textarea>
                            <span asp-validation-for="SanctionReason" class="text-danger" />
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="text-danger pull-left">
                        <i id="modalErrorIcon" class=""></i>
                        <span id="modalErrorText"></span>
                    </div>
                    <button type="submit" class="btn btn-primary">Save</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                </div>
            </div>
        </div>
    </div>
</form>

结果

使用Javascript调试会话

控制台日志

问题

  1. 为什么我的ModelState在我的Ajax响应未定义?

  2. 什么是使用JavaScript来显示服务器端验证后验证错误的呢? 我问这个,因为我会假设,如果一个用户拥有,因此客户端验证绕过禁用JavaScript我的服务器端验证只踢英寸 因此,如何能在JavaScript的执行,以显示验证错误服务器的ModelState返回,因为它仍然被禁用?

文章来源: Returning BadRequest in ASP.Net Core MVC to Microsoft jQuery Unobtrusive Ajax post has ModelState undefined