Saturday, 6 March 2021

MVC - Remote Validation - Button not included in POST data in Controller

 Watch this example on YouTube


 Button value lost in Remote Validator

 

to fix it replace

 @model ValidateOnDbSide.Models.Customer


@{ Html.EnableClientValidation();}
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "UserID" }))
{

    @Html.AntiForgeryToken()

    // @Html.CheckBoxFor(x => x.IsActive)
    @Html.DisplayNameFor(x => x.FirstName)
    @Html.TextBoxFor(x => x.FirstName)
    @Html.ValidationMessageFor(x => x.FirstName)
    <br />
    @Html.DisplayNameFor(x => x.LastName)
    @Html.TextBoxFor(x => x.LastName)
    @Html.ValidationMessageFor(x => x.LastName)
    <br />

     @Html.DisplayNameFor(x => x.Salary)
    @Html.TextBoxFor(x => x.Salary)
    @Html.ValidationMessageFor(x => x.Salary)
    <br />
    <input type="submit" value="Submit" name="btn" />
}

with 


@model ValidateOnDbSide.Models.Customer


@{ Html.EnableClientValidation();}
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "UserID" }))
{

    @Html.AntiForgeryToken()

    // @Html.CheckBoxFor(x => x.IsActive)
    @Html.DisplayNameFor(x => x.FirstName)
    @Html.TextBoxFor(x => x.FirstName)
    @Html.ValidationMessageFor(x => x.FirstName)
    <br />
    @Html.DisplayNameFor(x => x.LastName)
    @Html.TextBoxFor(x => x.LastName)
    @Html.ValidationMessageFor(x => x.LastName)
    <br />

     @Html.DisplayNameFor(x => x.Salary)
    @Html.TextBoxFor(x => x.Salary)
    @Html.ValidationMessageFor(x => x.Salary)
    <br />
    <input type="submit" value="Submit" name="btn" />
}

@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $('input[type="submit"]').click(function () {
                var hidden = $("<input type='hidden' />").attr("name", this.name).val(this.value).appendTo($(this).closest("form"));
            });
        });
    </script>    
    
}

1 comment:

  1. This article on MVC remote validation is very insightful! Just like flipper zero unleashed enhances your tech toolkit, understanding POST data issues can improve your application's reliability.

    ReplyDelete