6

I use AJAX to send forms to controller in ASP.NET Core but I have problem with send form with validation

<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

<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 ?

10
  • @mplungjan, OP is using the jquery.unobtrusive-ajax.js plugin (which makes an ajax call) Commented Jul 2, 2018 at 8:32
  • The code you have shown will work fine, and will not submit assuming you have a [Required] attribute on the Title property. Do you have all the relevant scripts loaded correctly? Commented Jul 2, 2018 at 8:34
  • @StephenMuecke Where can I see that? Commented Jul 2, 2018 at 8:35
  • @mahdi: PLEASE do not capitalise every word in your question. It is really irritating to read Commented Jul 2, 2018 at 8:35
  • 1
    That has nothing to do with client side validation! Commented Jul 2, 2018 at 8:38

1 Answer 1

3

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.

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.