0

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);
        }
2
  • Are you getting any error ? what doesn't work ? Commented Sep 27, 2020 at 4:49
  • No errors. It only returns back "Hello" but not whatever I typed in to the form added to the end of it. E.g. it's meant to say "Hello Bob". So the value is not ever arriving at the controller. Commented Sep 27, 2020 at 14:56

1 Answer 1

1

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

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

1 Comment

You Sir are a genius! Thank you so much! Yes I think the main issue was the var payload = {fn : fullName}; where the "fn" didn't match "fullName" in the controller method. You have no idea how much frustration you've saved me. Thank you a million times!

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.