-1

I have a form in my view which has only one textarea input initially and user can add more textarea inputs if he wants with jquery. My problem is related to second case. After submitting the form i am getting an array of objects in console but when i am passing this array to mvc action in my controller it is coming to be null. I have tried these solution but did not succeed:

Send array of Objects to MVC Controller

POST a list of objects to MVC 5 Controller

here is my code:-

jquery code:

$('body').on('submit', '#addTextForm', function () {
   console.log($(this));
   var frmData = $(this).serializeArray();
   console.log(frmData);
   $.ajax({
       type: 'post',
       url: '/Dashboard/UploadText',
       contentType: 'application/json',
       data: JSON.stringify({ obj: frmData }),
       success: function (data) {
          console.log(data);
       },
       error: function (data) {
          console.log(data);
       }
   });
  return false;
});

MVC action:

[HttpPost]
public string UploadText(SliderTextList obj)
{
  return "success";      
}

my object class:

public class SliderText
{
  [JsonProperty("Id")]
  public int Id { get; set; }

  [JsonProperty("SlideName")]
  public string SlideName { get; set; }

  [JsonProperty("Content")]
  public string Content { get; set; }

}
public class SliderTextList
{
  public List<SliderText> AllTexts { get; set; }
}

I have to store the Content in json file with Id and SlideName, so i think i have to pass a list object in mvc action Uploadtext which is coming out to be null always. Please help.

8
  • check in console post tab what data it is sending in Commented Feb 17, 2016 at 6:15
  • If you have generated the view correctly (i.e. the names or your dynamically created objects have the correct indexers), then all you need is data: $('#addTextForm').serialize() and remove contentType: 'application/json',.
    – user3559349
    Commented Feb 17, 2016 at 6:16
  • @StephenMuecke it still didn't worked Commented Feb 17, 2016 at 7:26
  • Then you have not generated the form controls correctly. Refer the answers here and here for some options for dynamically adding collection items. You have not shown your code so impossible to know what your doing wrong.
    – user3559349
    Commented Feb 17, 2016 at 7:29
  • 1
    But if all your editing is the Content property, then they could be all name="Content" but only if you change the method signature to [HttpPost]public ActionResultUploadText(IEnumerable<string> content) and then you build your collection of SliderText objects based on the the values in the array.
    – user3559349
    Commented Feb 17, 2016 at 7:42

2 Answers 2

1
$(document).ready(function(){
   $('body').on('submit', '#addTextForm', function () {
   var listData=[];
   var oInputs = new Array();
   oInputs = document.getElementsByTag('input' );
   var k=1;
   for ( i = 0; i < oInputs.length; i++ )
   {  
       if ( oInputs[i].type == 'textarea' )
       {
          var obj=new Object();
          obj.Id=k;
          obj.SlideName=oInputs[i].Name;
          obj.Content=oInputs[i].Value;
          K=parseInt(k)+1;
          listData.push(obj);
       }
   }
   $.ajax({
           type: 'post',
           url: '/Dashboard/UploadText',
           contentType: 'application/json',
           data: JSON.stringify(listData),
           success: function (data) {
                       console.log(data);
           },
           error: function (data) {
           console.log(data);
          }
     });
    return false;
   });
});
0

Since the sever side expects a string as argument obj as a query string I think this is what you need.

data: { 
    obj: JSON.stringify(frmData) 
},

as an alternative using as Stephen suggested on the comments

data: { 
    obj: $('#addTextForm').serialize()
},
3
  • No, that was not my suggestion - its just data: $('#addTextForm').serialize(), which will work if OP has generated the form controls correctly - ie. name=[0].SlideName", name=[1].SlideName etc
    – user3559349
    Commented Feb 17, 2016 at 6:30
  • @StephenMuecke I am not much familiar with ASP, but by the looks of it SliderTextList obj which is expecting a json string as the parameter. If we post data: $('#addTextForm').serialize() then the ajax page will be receiviving this arguments as 'id': idvalue, 'SlideName': SlideNameValue, 'Content': ContentValue, right? That thought made me make that adjustment.
    – rrk
    Commented Feb 17, 2016 at 6:41
  • No that's not correct. If the contentType: 'application/json' is removed, then the data will be posted as the default 'application/x-www-form-urlencoded; charset=UTF-8' which is the default expected by the DefaultModelBinder (and it will be bound correctly)
    – user3559349
    Commented Feb 17, 2016 at 6:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.