简体   繁体   English

Asp.net Core中带有Ajax的验证表

[英]Validation Form With Ajax in Asp.net Core

I use AJAX to send forms to controller in ASP.NET Core but I have problem with send form with validation 我使用AJAX将表单发送到ASP.NET Core中的控制器,但通过验证发送表单时遇到问题

<form asp-action="Create" asp-controller="Departments"
      data-ajax="true"
      data-ajax-method="Post"
      data-ajax-mode="replace"
      data-ajax-update="#content"                 
      data-ajax-success="Success"
      data-ajax-failure="Failure">
    <div class="box-body">
       <div class="alert alert-success" id="divalert" ></div>

        <div class="form-group">
            <label asp-for="Title" class="control-label"></label>
            <input asp-for="Title" class="form-control" />
            <span asp-validation-for="Title" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="ثبت" class="btn btn-success" />
        </div>
    </div>
</form>

This is Jquery code For Ajax 这是Ajax的Jquery代码

<script>
    function Success() {
        $("#divalert").text = "Yes";
    }
    function Failure() {
        $("#divalert").text = "No";

    }

</script>

but I want to show validation message ehen I send form with empty text Box , 但是我想在发送带有空文本框的表单时显示验证消息,

This is my Controller 这是我的控制器

public async Task<IActionResult> Create([Bind("Department_Id,Title,Task,Description,Department_Status")] Department department)
{
    if (ModelState.IsValid)
    {             
        _genericRepository.Add(department);
        await _genericRepository.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }

    return View(department);
}

How do I show validation message with Ajax when form is empty ? 当表单为空时,如何使用Ajax显示验证消息?

I use partial view "Create" and it works 我使用局部视图“创建”,并且有效

<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
<div class="form-horizontal">
    <form asp-action="Create" role="form" asp-controller="Department" asp-antiforgery="true"
          data-ajax-success="Bindgrid"
          data-ajax="true"
          data-ajax-method="POST">
        <div class="form-group">
            <label class="control-label" asp-for="DepartmentName"></label>
            <input class="form-control" type="text" asp-for="DepartmentName" />
            <span asp-validation-for="DepartmentName" class="text-danger"></span>
        </div>
        <input class="btn btn-primary" type="submit" value="Save" />
    </form>
</div>

all jquery file is needed for html or ajax validation. html或ajax验证都需要所有jquery文件。

I use this way and it worked 我用这种方式,它的工作

Is it how I should code it? 我应该怎么编码呢?

I change controller from IActionResult to JsonResult . 我将控制器从IActionResult更改为JsonResult。

public async Task<JsonResult> 
Create([Bind("Department_Id,Title,Task,Description,Department_Status")] 
Department department)
    {

        if (ModelState.IsValid)
        {             

            _genericRepository.Add(department);
            await _genericRepository.SaveChangesAsync();
            return Json(department);

        }

        return Json(department);
    }`

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

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