in Education by
I want pass values from my view to a controller action however every time i do the parameter is null. I've seen multiple posts about the same things but can't quite seem to get it right.. my code is below Controller Action [HttpPost] public IActionResult GetTest(string data) { var jSon = JsonConvert.SerializeObject(channles[0]); return Json(jSon); } View (AJAX) code function testAjax() { $("#searchButton1").click(function () { $.ajax({ type: "POST", url: '@Url.Action("GetTest", "DataGridWebApi")', data: { data: "test" }, contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); } I hit the breakpoint in my controller action but the parameter is always null. I've also tried changing my controller action to accept an object instead of a string but nothing seems to work. What am i doing wrong? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You need to make two changes. You do need [FromBody] on the param: public IActionResult GetTest([FromBody]string data) Then, you need to send just the string, as JSON: data: JSON.stringify("test") That may seem a little weird, but without JSON.stringify it will be sent as just test, and you need "test". In short, you're sending a string with a JSON content type, so it must be a JSON string.

Related questions

0 votes
    I want pass values from my view to a controller action however every time i do the parameter is null ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Say I have a page that display search results. I search for stackoverflow and it returns 5000 results, 10 per page. Now I ... was posted previously etc.. What I'd like to do is...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    Has anybody established a good naming convention for action in MVC? I was specifically looking at ASP.net ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    Say I have a page that display search results. I search for stackoverflow and it returns 5000 results, 10 per page. Now I ... was posted previously etc.. What I'd like to do is...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I have batch of user accounts and every user account's password is not crypted.I want to insert to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have implemented exception handling as mentioned in below link How to pass error message to error view in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have two submit buttons and a few textboxes on my page. When I enter details in the textbox and hit ... model.FirsttName) @Html.TextBoxFor(model => model.LastName) Get History...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Description: I have a solution in Visual Studio which has multiple projects as application layers. Business Logic ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have two submit buttons and a few textboxes on my page. When I enter details in the textbox and hit ... model.FirsttName) @Html.TextBoxFor(model => model.LastName) Get History...
asked May 17, 2022 in Education by JackTerrance
0 votes
    Description: I have a solution in Visual Studio which has multiple projects as application layers. Business Logic ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    I have two submit buttons and a few textboxes on my page. When I enter details in the textbox and hit ... model.FirsttName) @Html.TextBoxFor(model => model.LastName) Get History...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have an XML document which basically looks like this: SomeSeller 2535 Now I'm trying to parse the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I've started to work a bit with master pages for an ASP.net mvc site and I've come across a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I've started to work a bit with master pages for an ASP.net mvc site and I've come across a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    If I want to use the validation framework that you can use with ASP.NET MVC, will the JavaScript ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
...