ASP.NET Core MVC远程验证未命中控制器

时间:2019-10-31 12:36:14

标签: c# jquery html asp.net-core-mvc remote-validation

我正在以局部视图的形式使用内核的远程验证,该视图在主视图中加载,但是在尝试执行验证时,浏览器控制台中始终出现错误([element_name]是字段的名称正在验证):

  

未捕获的TypeError:无法读取未定义的属性“ call”。检查时发生异常   元素 [element_name] ,请选中“ __dummy__”方法。
      在a.validator.check(VM23 jquery.validate.min.js:4)
      在a.validator.element处(VM23 jquery.validate.min.js:4)
      在a.validator.onfocusout处(VM23 jquery.validate.min.js:4)
      在HTMLTextAreaElement.b(VM23 jquery.validate.min.js:4)
      在HTMLFormElement.dispatch(jquery.min.js:2)
      在HTMLFormElement.v.handle(jquery.min.js:2)处
      在Object.trigger(jquery.min.js:2)
      在Object.simulate(jquery.min.js:2)
      在HTMLDocument.i(jquery.min.js:2)


因此,我有一个View,单击此按钮后,会在此<div>中加载包含填充表格(执行更新操作)的局部视图:

<div class="modal fade" id="modalPlaceholder" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="false"></div>

使用以下jQuery代码:

function requestUpdate(idSolicitacao) {
    $('#modalPlaceholder').empty();
    $.ajax({
        type: 'POST',
        url: 'Solicitacao/GetUpdate/' + idSolicitacao,
        success: function (result) {
            $('#modalPlaceholder').html(result);
        }
    });     
    $('#modalPlaceholder').modal('show');
}

在部分视图表单中,我正在使用远程验证来验证用户所做的更改。我在此处包括了jquery-validate脚本,因为当我仅将其导入_Layout.cshtml文件中时,该脚本不起作用。整个文件如下所示:

@model Potinho.ViewModels.SolicitacaoVM.Update
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
                <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title"><strong>Solicitação</strong></h4>
        </div>
        <div class="container-fluid" style="margin:10px">
            <div class="row">
                <div class="col">
                    <form asp-controller="Solicitacao" asp-action="Update" method="post">
                        <div class="form-group">
                            <input asp-for="@Model.IdSolicitacao" hidden />
                            <input asp-for="@Model.IdPote" hidden />
                            <input asp-for="@Model.IdSolicitante" hidden />
                            <input asp-for="@Model.IdFavorecido" hidden />
                            <input asp-for="@Model.IdItem" hidden />
                            <input asp-for="@Model.IdStatus" hidden />
                            <div class="mb-2">
                                <label asp-for="@Model.Descricao" class="control-label"></label>
                                <textarea asp-for="@Model.Descricao" class="form-control"> </textarea>
                                <span asp-validation-for="@Model.Descricao" class="text-danger"></span>
                            </div>
                            <div class="mb-2">
                                <label asp-for="@Model.Local" class="control-label"></label>
                                <input asp-for="@Model.Local" class="form-control" />
                                <span asp-validation-for="@Model.Local" class="text-danger"></span>
                            </div>
                            <div class="mb-2">
                                <label asp-for="@Model.Valor" class="control-label"></label>
                                <input asp-for="@Model.Valor" class="form-control" />
                                <span asp-validation-for="@Model.Valor" class="text-danger"></span>
                            </div>
                            <div class="mb-2">
                                <label asp-for="@Model.DataInicio" class="control-label"></label>
                                <input asp-for="@Model.DataInicio" class="form-control" type="date" />
                                <span asp-validation-for="@Model.DataInicio" class="text-danger"></span>
                            </div>
                            <div class="mb-2">
                                <label asp-for="@Model.DataFim" class="control-label"></label>
                                <input asp-for="@Model.DataFim" class="form-control" type="date" />
                                <span asp-validation-for="@Model.DataFim" class="text-danger"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <input type="submit" value="Salvar" class="btn btn-success" />
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"></script>
</div>

ViewModel的相关部分如下:

public class Update
{
    [BindProperty]
    [Remote(action: "IsDescricaoValid", controller: "Solicitacao")]
    [Display(Name = "Descrição")]
    public string Descricao { get; set; }

    [BindProperty]
    [Remote(action: "IsLocalValid", controller: "Solicitacao")]
    public string Local { get; set; }

    [BindProperty]
    [Remote(action: "IsValorValid", controller: "Solicitacao")]
    public decimal Valor { get; set; }

    [BindProperty]
    [Remote(action: "IsDataInicioValid", controller: "Solicitacao")]
    [Display(Name = "Data de início")]
    public DateTime DataInicio { get; set; }

    [BindProperty]
    [Remote(action: "IsDataFimValid", controller: "Solicitacao")]
    [Display(Name = "Data final")]
    public DateTime DataFim { get; set; }

    [Required]
    [BindProperty]
    public DateTime DataSolicitacao { get; set; }

    [Key]
    [Required]
    [BindProperty]
    public Guid IdSolicitacao { get; set; }

    [Required]
    [BindProperty]
    public Guid IdPote { get; set; }

    [Required]
    [BindProperty]
    public string IdSolicitante { get; set; }

    [Required]
    [BindProperty]
    public string IdFavorecido { get; set; }

    [Required]
    [BindProperty]
    public Guid IdItem { get; set; }

    [Required]
    [BindProperty]
    public Guid IdStatus { get; set; }
}

以及控制器中的远程验证方法:

[AcceptVerbs("Get", "Post")]
[AllowAnonymous]
public IActionResult IsDescricaoValid(string descricao)
{
    return _validator.IsDescricaoValid(descricao);
}

[AcceptVerbs("Get", "Post")]
[AllowAnonymous]
public IActionResult IsLocalValid(string local)
{
    return _validator.IsLocalValid(local);
}

[AcceptVerbs("Get", "Post")]
[AllowAnonymous]
public IActionResult IsValorValid(decimal valor)
{
    return _validator.IsValorValid(valor);
}

[AcceptVerbs("Get", "Post")]
[AllowAnonymous]
public IActionResult IsDataInicioValid(DateTime dataInicio)
{
    return _validator.IsDataInicioValid(dataInicio);
}

[AcceptVerbs("Get", "Post")]
[AllowAnonymous]
public IActionResult IsDataFimValid(DateTime dataFim)
{
    return _validator.IsDataFimValid(dataFim);
}

根据我的测试,<div>正在加载部分视图,并且导入了脚本,但是每个字段都会触发相同的错误,而我放置在控制器中的断点甚至都没有被击中。

1 个答案:

答案 0 :(得分:0)

解决了。我正在使用过时的(或有缺陷的)jquery-validate导入。为了解决这个问题,我替换了行

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.js"></script>

_UpdateSolicitacao.cshtml文件中。