0

I want to pass array value from view to the controller action using ajax. I have created jquery and controller method as bellow, but its not working.

in my javascript file,

$("#btnSave").click(function () {
    var data = [];
    $("#utiltyTable tr.maintr").each(function () {

        var selectedMail = [];
        var selectedMobile = [];

        var categoryId = $(this).find("td.uCategory").find("input[type='hidden']").val();

        $(this).find("td.uEmail").find("select :selected").map(function (i, el) {
            selectedMail.push({ "value": $(el).val(), "item": $(el).text() });
        });

        $(this).find("td.uSMS").find("select :selected").map(function (i, el) {
            selectedMobile.push({ "value": $(el).val(), "item": $(el).text() });
        });

        data.push({ categoryId: categoryId, selectedMail: JSON.stringify(selectedMail), selectedMobile: JSON.stringify(selectedMobile) })


    });

    console.log(data);

    $.ajax({
        url: '@Url.Action("UtilityEmailSMS", "Account")',
        type: 'POST',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(data),
        success: function (data) {

        }
    });

});

Controller action method as bellow,

       [HttpPost]
    public ActionResult UtilityEmailSMS(string[] data)
    {

        return View();
    }

I am getting string[] array.but all key value as null enter image description here

3
  • But you try to pass array of objects, not array of string. Commented Sep 21, 2017 at 8:41
  • if i use public ActionResult UtilityEmailSMS(object[] data) i am also getting null in data Commented Sep 21, 2017 at 8:42
  • Its just selectedMail: selectedMail (not JSON.stringify(selectedMail)) and ditto forselectedMobile: selectedMobile. You need a model to bind to that matches the data you sending Commented Sep 21, 2017 at 8:43

2 Answers 2

1

You should accept array of objects instead of string, as you are passing object array from ajax.

So create a model for same.

public class Mail
  {
    public string value {get;set;}
    public string item {get;set;}
  }

public class Test
{
    public string categoryId {get;set;}
    public Mail[] selectedMail {get;set;}
    public Mail[] selectedMobile {get; set;}
}

Then

    [HttpPost]
    public ActionResult UtilityEmailSMS(Test[] data)
    {

        return View();
    }
Sign up to request clarification or add additional context in comments.

7 Comments

OP's data is not going to bind to that model!
@StephenMuecke : fixed.
Not yet :) selectedMail is a collection of an model containing properties Item and Value (ditto for selectedMobile)
@StephenMuecke : but see OP has parsed that object into string. selectedMail: JSON.stringify(selectedMail)
And I noted that is incorrect in my comment above :)
|
0

You can use JavaScriptSerializer as you are using JSON.stringify() it will pass data in string format to you action method, Instead of string[] parameter use string in you action method

// Custom class
public class Test
{
    public string categoryId {get;set;}
    public List<Mail> selectedMail {get;set;}
    public List<Mail> selectedMobile {get; set;}
}

 public ActionResult UtilityEmailSMS(string data)
 {
      JavaScriptSerializer serializer = new JavaScriptSerializer();
      var list= serializer.Deserialize<List<Test>>(data);
      return View();
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.