We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

How to prevent form from being submitted?

Temody Mas
Temody Mas asked
on
38 Views
Last Modified: 2020-08-14
Hi, I have MVC project my  code need to check that this code exist in data base if exist prevent submission else submit normally  I try  make it with Json
this is my Razor View

@using (Html.BeginForm(actionName: "Create",
                  controllerName: "Employee",
                  method: FormMethod.Post,
                  htmlAttributes: new { enctype = "multipart/form-data", @id = "form-horizontal", @class = "form-horizontal form-wizard-wrapper text-right form-parsley" }

                 ))
{

    <div class="col-md-6">
        <div class="form-group row">
            <label for="txtAddress2Billing" class="col-lg-3 col-form-label">Code</label>
            <div class="col-lg-8 form-group" id="classcode">
                @Html.EditorFor(model => model.code, new { htmlAttributes = new { @type = "Text", @class = " form-control", @id = "Code", @oninput = "CodeCheck()" } })
                @Html.ValidationMessageFor(model => model.code, "", new { @class = "text-danger" })
            </div>
            <p id="Status" class="col-lg-1 mt-2" />
        </div><!--end form-group-->
    </div><!--end col-->

    <div class="form-group row">
        <div class="col-lg-2"></div>
        <div class="col-lg-3">
            <input type="submit" value="Create" class="btn btn-outline-warning" style="width:150px;" />
        </div>
        <div class="col-lg-3">
            <a href="~/EmpDatas/Index" class="btn btn-outline-warning" style="width:150px"> Cancel</a>
        </div>
    </div>

}




<script src="~/assets/js/jquery.min.js"></script>
<script src="~/assets/js/bootstrap.bundle.min.js"></script>
<script src="~/assets/js/app.js"></script>

<script>


      function CodeCheck() {
          $("#Status").html('<i class="fas fa-spinner fa-pulse"></i>');
        $.post("@Url.Action("CheckCodeAvailability", "Employee")",
            {
                Code: $("#Code").val()
            },
        function (data) {
            if (data == 0) {

                $("#Status").html('<i class="far fa-times-circle text-danger"></i>');
                $("#Code").css("border-color", "Red");
               
               
            }
            else {
                $("#Status").html('<i class="far fa-check-circle text-success"></i>');
                $("#Code").css("border-color", "Green");
                
                
            }
        });
    }
</script>

and This is my Json code

public JsonResult CheckCodeAvailability(string Code)
        {
            System.Threading.Thread.Sleep(200);

            var exist = db.EmpDatas.Where(m => m.code.ToString() == Code).Count();

            if (exist < 1)
            {
                return Json(1);
            }
            else
            {
                return Json(0);
            }
           
        }

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE