kaushik guru

kaushik guru

  • NA
  • 252
  • 30.8k

ajax always returns null

Mar 29 2023 1:18 PM

On a button click event i am trying to pass a textbox value to a controller . I am able to view the text box values by using console but in controller always returns null

this is my razor code

@{
    ViewData["Title"] = "Index";
    Layout = "Commonlayout";
}
<head>


    <script type="text/javascript">
        $(document).ready(function () {
            var lol = document.getElementById("report").value;
            console.log(lol);
            $("#generate").click(function () {
                $.ajax({
                    type: "POST",
                    url: "/HRDashboard/getresult",
                    data: { //Passing data
                        name: $("#txtName").val()
                    }
                    
                });
            });
        });
    </script>
    
</head>
@*<h1>Index</h1>*@

@*<p>
    <a asp-action="Create">Create New</a>
</p>*@
<body>
    <h1 style="text-align:center">To be Validated</h1>
    &nbsp;
    <div class="container">
        <div class="row">
                <div class="row mb-2">

                    <label class="col-sm-2 col-form-label">Report Type</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control"  id="report" ></input>
                        
                    </div>
                    <div>
                        <input type="button" value="Download" id="generate" class="btn btn-primary" />
                    </div>
                </div>
        </div>
    </div>
</body>

and my controller is as follows

public JsonResult getresult(string name)
{
}

I am not sure where i am wrong please guide.


Answers (2)