0

I want to fetch data from the database whenever the user selects a value from drop down. For these purpose I have written a block for code, in which a JavaScript function is call whenever a user selects a value from drop down and passes that value to the controller through ajax. Below is the snapshot of the code.

cshtml

@Html.DropDownList("Code", null, htmlAttributes: new { @class = "form-control", @onchange = "fetchData(this.value)" }) 

JS

<script>
    function fetchData(Code) {

        $.ajax({
            url: '@(Url.Action("fetchCode", "ChargesSetup"))',
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'Code':'" + Code + "'}",
            contentType: false,
            processData: false,
            success: function (result) {
                console.log(result);
            },
            error: function (result) {
                console.log("E");
            }
        });
    }
</script>

NOTE: I have tried my format for passing data to controller, the value of code inside JavaScript is correct as 'A'. But in controller class is show as @p__linq__0. when I hard code the value of code the controller work fine.

Controller

[HttpPost]
public JsonResult fetchData(string Code)
{
      IQueryable chargestable = from p in db.chargestable where (p.Code == Code) select p;
      return Json(chargestable , JsonRequestBehavior.AllowGet);
}

In JavaScript the value of drop down is exactly same what we want:

Value of drop down in JS

but after AJAX call, in controller the value of drop down is change to some thing like @p__linq__0

{SELECT 
[Extent1].[area] AS [area]
FROM [dbo].[chargestable] AS [Extent1]
WHERE [Extent1].[Code] = @p__linq__0}

What is problem with my code? How can I resolve this as I need to get the buffer of chargestable whenever drop down value is selected.

By changing return statement makes no effect because the value of parameter is not set, its always null.

return Json(chargestable.ToList(), JsonRequestBehavior.AllowGet);

Value of Parameter

Alishah momin
  • 114
  • 1
  • 10
  • What you are seeing in that last code block is the SQL generated by your LINQ statement. I think what you want is to do `return Json(chargestable.ToList(), JsonRequestBehavior.AllowGet);`. That will execute the `IQueryable` and return the data. – Heretic Monkey May 04 '18 at 18:38
  • return null object. Object AsyncState : null CreationOptions : 0 Exception : null Id : 107 IsCanceled : false IsCompleted : true IsFaulted : false Result : Array(0) length : 0 __proto__ : Array(0) Status : 5 __proto__ : Object – Alishah momin May 04 '18 at 18:42
  • Well, sure, it's not finding any matches, so it's null. I think the whole `@p__linq__0` thing is a red herring. To get the parameter, you might try adding `[FromBody]` in your controller method: `fetchData([FromBody] string Code)`. – Heretic Monkey May 04 '18 at 19:05
  • You should also see if `Code` is defined in your JavaScript function as well... – Heretic Monkey May 04 '18 at 19:05
  • First of all, an entry is found against the select value in the table. secondly the code variable is also defining in JavaScript and a value of its also output and attach in above question description. – Alishah momin May 04 '18 at 19:13
  • Okay... that's not at all clear in the question. If the picture attached there is indeed the value of the JavaScript `Code` variable, it shows an empty array. Your controller function takes a string. Of what type is the `Code` column on your `chargestable` database table? – Heretic Monkey May 04 '18 at 19:18
  • it's varchar. The first line on picture show the value of Code in JS and second line empty array, is the result comes from controller class. console.log(result) line in success: function (JavaScript). – Alishah momin May 04 '18 at 19:33
  • Try console.log(result.d); in you success. https://stackoverflow.com/questions/3927851/why-do-i-need-to-use-d-to-access-data-returned-by-jquery-ajax – Eric May 05 '18 at 01:17

0 Answers0