2

I do not have access to input values when using Ajax in View(MVC) but I have access to input values when not use Ajax. actually values is empty when use Ajax

When I use Ajax:

<form id="Form1" asp-action="Register" asp-controller="UserPanel" method="post">
    <div class="row">
        <div class="col-lg-5 col-md-5 col-sm-12 col-12  .sm-right" id="margin-top">
            <span>Name:</span>
            <input type="text" asp-for="Name" class="form-control">
        </div>
        <div>
            <span>Number:</span>
            <input type="text" asp-for="Number" class="form-control">
        </div>
    </div>
    <input type="reset" value="send" id="ajax-button" class="btn btn-success btn-sm waves-effect waves-light submit-btn" />
</form>

Script:

<script>
    $(document).ready(function () {
        var url = '@Url.Action("Register", "UserPanel")';
        var data = $('#Form1').serialize();
        $('#ajax-button').click(function () {
            debugger
            $.ajax({
                type: "POST",
                data: data,
                url: url,
                contentType: 'application/json; charset=utf-8',
                success: function (result) {
                    alert('Done');
                },
                error: function () {
                    alert("error");
                }
            });
        })
    })
</script>

I added tag helpers

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, SmsWebClient
@using Microsoft.AspNetCore.Razor.TagHelpers;

Value is null before enter to ajax ,image:

enter image description here

3
  • Post the controller action method Commented Apr 28, 2019 at 20:18
  • Value is null above enter to ajax. @KirkLarkin Commented Apr 29, 2019 at 4:32
  • You have to serialize the form in button click event. Commented Apr 29, 2019 at 5:26

3 Answers 3

3

Please Move the

var data = $('#Form1').serialize();

to below

$('#ajax-button').click(function () {

In fact, your code should be:

<script>
    $(document).ready(function () {
        var url = '@Url.Action("Register", "UserPanel")';
        $('#ajax-button').click(function () {
        var data = $('#Form1').serialize();
            debugger
            $.ajax({
                type: "POST",
                data: data,
                url: url,
                success: function (result) {
                    alert('Done');
                },
                error: function () {
                    alert("error");
                }
            });
        })
    })
</script>

In your code data set when docuement loaded and for this reason the value is null

Sign up to request clarification or add additional context in comments.

Comments

1

You must write like this

<form id="Form1" asp-action="Register" asp-controller="UserPanel" method="post">
    <div class="row">
        <div class="col-lg-5 col-md-5 col-sm-12 col-12  .sm-right" id="margin-top">
            <span>Name:</span>
            <input type="text" asp-for="Name" class="form-control">
        </div>
        <div>
            <span>Number:</span>
            <input type="text" asp-for="Number" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <button id="ajax-button">Submit</button>
    </div>
</form>

And This ViewModel

 public class RegisterVm
    {
        public string Name { get; set; }
        public string Number { get; set; }
    }

And finally this is the Ajax code

@section Scripts{
    <script>
        $(document).ready(function () {

            var url = $('#Form1').attr("action");

            var model = $('#Form1').serialize();

            var token = $('input[name=__RequestVerificationToken]').val();

            model.__RequestVerificationToken = token;

            $('#ajax-button').click(function () {
                $.ajax({
                    type: $('#Form1').attr("method"),
                    data: model,
                    url: url,
                    success: function (result) {
                        alert('Done');
                    },
                    error: function () {
                        alert("error");
                    }
                });
            })
        })
    </script>
}

Comments

0

Your data are set when docuement loaded, it will not pass the value which you enter.

For a working demo, try code below:

<script>
    $(document).ready(function () {
        $('#ajax-button').click(function () {
            var url = '@Url.Action("Register", "Home")';
            var data = new FormData();
            data.append('Name', $('#Name').val());
            data.append('Number', $('#Number').val());

            $.ajax({
                type: "POST",
                data: data,
                url: url,
                processData: false,
                contentType: false,
                success: function (result) {
                    alert('Done');
                },
                error: function () {
                    alert("error");
                }
            });
        })
    })
</script>

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.