Answered by:
JSON data empty on Controller

Question
-
User1509050366 posted
Hi
My JSON data on controller is empty… not sure what I’m doing wrong
My View code
@{ ViewBag.Title = "Create"; } <script type="text/javascript"> $(document).ready(function () { $("#GenerateR").click(function () { debugger; var FRate = {}; FRate.ServiceType = $("#ServiceTypeId option:selected").text(); FRate.OriginLocationCity = $("#OriginLocationCity option:selected").text(); FRate.OriginLocationState = $("#OriginLocationState option:selected").text(); var data = JSON.stringify(FRate); $.ajax({ type: 'POST', url: '@Url.Action("SaveF", "FRateHeaders")', contentType: 'application/json; charset=utf-8', dataType: 'json', data: data, error: function (err) { alert("error - " + err); } }); });
My controller data
[HttpPost] public ActionResult SaveF(string header) { //do something with the data return null; }
I have also tried to ... but same output
[HttpPost] public ActionResult SaveF(EntityType header) { //do something with the data return null; }
Wednesday, July 24, 2019 4:01 PM
Answers
-
User475983607 posted
This happens when the form input names do not match the action input parameter property names.
It is impossible to provide an accurate solution, since we cannot see the latest source code,
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 24, 2019 5:43 PM
All replies
-
User-474980206 posted
in the controller you specify the post data as a string, but in the ajax call you are passing a son object. try:
public class SaveFRq { public string ServiceType {get; set;} public string OriginLocationCity {get; set;} public string OriginLocationState {get; set;} } [HttpPost] public ActionResult SaveF(SaveFRq rq) { //do something with the data return null; }
Wednesday, July 24, 2019 4:20 PM -
User753101303 posted
Hi,
And the definition for EntityType is ? Does it have matching public property names (ie ServiceType, OriginLocationCity and OriginLocationState ?)
Maybe not related but when I don't want to return anything I'm using void (and ASP.NET should return a 204 no content response for you). What you see is not crystal clear as you talk about an "empty controller" or "having the same output" but for now my guess is that you can't read the posted data ?
Wednesday, July 24, 2019 4:23 PM -
User1509050366 posted
Should the entity be a 100% match....
For example from view I'm returning 3 properties but the entity has 7 more then is that okay...
public class SaveFRq { public string ServiceType {get; set;} public string OriginLocationCity {get; set;} public string OriginLocationState {get; set;} }
Actual SaveFQ is as below...
public class SaveFRq { public string ServiceType {get; set;} public string OriginLocationCity {get; set;} public string OriginLocationState {get; set;}
public string CreatedDate{get; set;}
public string CreatedBy {get; set;} }Wednesday, July 24, 2019 4:29 PM -
User1509050366 posted
Thanks @PatriceSc
Problem statement: When data is posted on controller it doesn't contain anything... its empty.. all nulls...
Wednesday, July 24, 2019 4:38 PM -
User475983607 posted
This happens when the form input names do not match the action input parameter property names.
It is impossible to provide an accurate solution, since we cannot see the latest source code,
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 24, 2019 5:43 PM -
User1509050366 posted
Looks like that was the issue...
To resolve I created a seperate class and add only those property that were part of response from view ...
Thanks for your help
Wednesday, July 24, 2019 9:50 PM -
User1520731567 posted
Hi singhswat,
According to your two picture:
I suggest you could keep parameters names consistent between ajax and action,for example:
modify this line:
data: JSON.stringify({ 'FxxxHeader': FxxxHeader}),
to:
data: JSON.stringify({ 'header': FxxxHeader}),
Because the parameter name is also header in action.
Best Regards.
Yuki Tao
Thursday, July 25, 2019 5:05 AM