agent_smith1984
agent_smith1984

Reputation: 253

Pass data from view to controller using AJAX in C# ASP.NET Core MVC

I have tried looking at answers to similar questions and none of them work for my code. I have tried a lot of different things, all it should do is post the fullname and then display it back in the view. The view code:

    <script type="text/javascript">
        $(document).ready(function () {<script type="text/javascript">
        $(document).ready(function () {

$('#buttonDemo2').click(function () {
                var fullName = $('#fullName').val();
                var payload = {fn : fullName};
                $.ajax({
                    type: 'POST',
                    url: '/demo/demo2/',
                    contentType: 'application/json',
                    data: JSON.stringify(payLoad),
                    success: function (result) {
                        $('#result2').html(result);
                    }
                });
            });
</script>

<fieldset>
        <legend>Demo 2</legend>
        Full Name <input type="text" id="fullName" />
        <input type="button" value="Demo 2" id="buttonDemo2" />
        <br />
        <span id="result2"></span>
</fieldset>

The controller code:

 [HttpPost]
    public IActionResult Demo2(string fullName)
        {
            return new JsonResult("Hello " + fullName);
        }

Upvotes: 0

Views: 3143

Answers (1)

LouraQ
LouraQ

Reputation: 6901

First, when you pass string by ajax to action,you should ensure the received parameter name is the same as the incoming parameter name.

So, you should change var payload = {fn : fullName}; to var payload = {fullName: fullName};, or change public IActionResult Demo2(string fullName) to public IActionResult Demo2(string fn).

Then, because you passed only a string not an object parameter , so you don't need to use JSON.stringify, and remove contentType: 'application/json' .

Here is the detailed code:

    <script type="text/javascript">
            $(document).ready(function () {

                $('#buttonDemo2').click(function () {
                    var fullName = $('#fullName').val();
                    var payload = { fullName: fullName }; // change name
                    $.ajax({
                        type: 'POST',
                        url: '/demo/demo2/',
                       // contentType: 'application/json', // remove this line
                        data: payload, //remove JSON.stringify
                        success: function (result) {
                            $('#result2').html(result);
                        }
                    });
                });
            });
    </script> 
<fieldset>
    <legend>Demo 2</legend>
    Full Name <input type="text" id="fullName" />
    <input type="button" value="Demo 2" id="buttonDemo2" />
    <br />
    <span id="result2"></span>
</fieldset>

Controller:

 [HttpPost]
        public IActionResult Demo2(string fullName)
        {
            return new JsonResult("Hello " + fullName);
        }

Here is the test result:

enter image description here

Upvotes: 1

Related Questions