Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just create simple mvc project.

But in controller, parameter value pSearch is not display in debugger mode.
why it is?
what with wrong above code???
please help me any body proper jquery coding way.

What I have tried:

I have to search to internet as follows in below
Razor view code:
------------
<script>
        $(document).ready(function () {
            OnTextKeydownHandler();
        });

        function OnTextKeydownHandler() {
            $('#myTxtSearch').keyup(function () {
                MySearch();
            });
        };

        function MySearch() {
            var myTxt = $('#myTxtSearch').val();
            var myUrl = '/Unit/MySearch'// + myTxt;

            $.ajax({
                url: myUrl,
                type: "POST",
                datatype: "json",
                data: { 'name' : myTxt }
            });

        }

</script>

<div class="center" style="width:400px; border:1px solid black; margin-top:100px;">
    <h4 style="text-align:center">Create MyView</h4>
    <div>
        MySearch: <input type="text" id="myTxtSearch">
    </div>
</div>

Controller Code :
--------------
public ActionResult MySearch(string pSearch)
{
    return View();
}
Posted
Updated 25-Nov-19 1:39am

1 solution

you do not need to pass parameter at last in URL. so keep url like

var myUrl = '/Unit/MySearch';


And in post method, with data parameter, the json key should be "pSearch", so it should look like,

$.ajax({
     url: myUrl,
     type: "POST",
     datatype: "json",
     data: { 'pSearch' : myTxt }
 });


Some examples,
Posting Data to an MVC Controller With jQuery – bitScry[^]
jQuery AJAX Call to MVC Controller with Parameters - Sensible Dev[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900