1

I'm using asp.net mvc4 i create an array of array in jquery that is not passed to the controller here is my jquery code

 var tblHeader = new Array();
            var customers = new Array();
             $("#detailtbl THEAD TR").each(function () {
                var row = $(this);
                var header = [];
                for (var i = 0; i < row.find("TH").length; i++) {
                    header[i] = row.find("TH B").eq(i).html();
                }
                tblHeader.push(header);
            });
             $("#detailtbl TBODY TR:not(:last-child)").each(function (index) {
                 if (index > 3) {
                    var row = $(this);
                    var customer = [];
                    var hd = tblHeader[0];
                    for (var i = 0; i < row.find("TD").length; i++) {
                        var hrd = hd[i];
                        customer[hrd] = row.find("TD").eq(i).html();
                    }
                    customers.push(customer);
                 }
            });

here is my ajax call

$.ajax({
                        type: "POST",
                        url: '@Url.Action("GenerateTC", "TC")',
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify({ "tm": sendingvalue,"tcval":customers}),
                        success: function (data) {
                            if (data == 1 || data == -1) {
                                if (btnevent == "Save")
                                {
                                    toastr.success('', 'TC Generate Successfully!!', {
                                        timeOut: 1500,
                                        fadeOut: 1500,
                                        onHidden: function () {
                                            window.location.reload();
                                        }
                                    });
                                } else {
                                    toastr.success('', 'TC Updated Successfully!!', {
                                        timeOut: 1500,
                                        fadeOut: 1500,
                                        onHidden: function () {
                                            window.location.reload();
                                        }
                                    });
                                }
                            }
                            else {
                                toastr.error("Something went to wrong");
                            }
                        },
                        //error: function () { alertify.alert('Error. Please try again.'); }
                    });

here is my controller code

[HttpPost]
    public ActionResult GenerateTC(TCMain tm, object[] tcval)
    {
return view();
}

here is my object

public partial class TCMain
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public TCMain()
    {
        this.TCStd = new HashSet<TCStd>();
        this.TCVal = new HashSet<TCValue>();
        this.SubChall = new HashSet<subChallan>();
        this.CertiMaster = new HashSet<TestCertificateMaster>();
    }

    public int ID { get; set; }
    public string TCNO { get; set; }
    public Nullable<System.DateTime> TCDT { get; set; }
    public string PCODE { get; set; }
    public string ChallanNo { get; set; }
    public Nullable<int> METALCode { get; set; }
    public string HTCondition { get; set; }
    public string Note1 { get; set; }
    public string Note2 { get; set; }
    public string Note3 { get; set; }
    public string Note4 { get; set; }
    public string Note5 { get; set; }
    public string Note6 { get; set; }
    public string Note7 { get; set; }
    public string Note8 { get; set; }
    public string Note9 { get; set; }
    public string Note10 { get; set; }
    public string SI1 { get; set; }
    public string SI2 { get; set; }
    public string SI3 { get; set; }
    public string SI4 { get; set; }
    public string SI5 { get; set; }
    public string SI6 { get; set; }
    public string SI7 { get; set; }
    public string SI8 { get; set; }
    public string SI9 { get; set; }
    public string SI10 { get; set; }
    public string Marking { get; set; }
    public string Remarks { get; set; }
    public string PHYStats { get; set; }
    public Nullable<int> TCRaisetoOther { get; set; }
    public string TCConsignee { get; set; }
    public string TCAdd1 { get; set; }
    public string TCAdd2 { get; set; }
    public string TCAdd3 { get; set; }
    public Nullable<int> TCFromChallan { get; set; }
    public string TCFromOA_select { get; set; }
    public List<object> TCvalues { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<TCStd> TCStd { get; set; }
    public virtual ICollection<TCValue> TCVal { get; set; }
    public virtual ICollection<subChallan> SubChall { get; set; }
    public virtual ICollection<TestCertificateMaster> CertiMaster { get; set; }
}

an array is passed from ajax but doesn't get on the controller side. here I upload of ajax passed value and controller get value

AjaxPassed Value Conroller Get Value

here JSON response in the console

JSON Response Imnage

can anyone have a solution then please help me. thank you in advance

10
  • I just realized, looking at your print screen, that each item of you array is another array. And not a object. Can you share a plain text json that is sent on your request? Commented Dec 9, 2019 at 15:04
  • @Lutti Coelho Ya Sure, I update my question with JSON screenshot Commented Dec 10, 2019 at 3:47
  • Could you try updating controller action signature to public ActionResult GenerateTC(TCMain tm, List<List<object>> tcval) Commented Dec 10, 2019 at 4:10
  • I tried it now but it doesn't work :( Commented Dec 10, 2019 at 4:15
  • Try updating controller action signature to public ActionResult GenerateTC(TCMain tm, dynamic tcval) Commented Dec 10, 2019 at 11:39

2 Answers 2

1

I think you need to define a complex object Like this :

public class MyModel
{
  public TCMain tm {get;set;}
  public List<MyArray> tcval{get;set;} 
}

And the MyArray object should be like this :

public class MyArray
 {
   public string PONO{get;set;}
   public string Dieno{get;set;}
   // and other properties

 }

And here your action :

[HttpPost]
Public ActionResult GenerateTC(MyModel model)

And your JSON setting data section :

var rawData = {"tm" : sendingValue, "tcval" : customers};
data : JSON.stringify({"model":rawData}),

. . . .

I checked the code again, and finally I got value in the action by changing the below part :

$("#detailtbl TBODY TR:not(:last-child)").each(function (index) {
             if (index > 3) {
                var row = $(this);
                var customer = []; // ================> var customer ={};
                var hd = tblHeader[0];
                for (var i = 0; i < row.find("TD").length; i++) {
                    var hrd = hd[i];
                    customer[hrd] = row.find("TD").eq(i).html();
                }
                customers.push(customer);
             }
        });

Now, to cast the parameter value to your model you need to use specific ModelBinder or do it manually for this specific action

Sign up to request clarification or add additional context in comments.

11 Comments

but my properties are dynamic (no specific property in MyArray)
So just change this property like this : public List<object> tcval{get;set;} because of this, you need to cast it to the MyArray after you get the value in the action
still not getting the value in the controller :(
As @Lutti said, try this = > data : {"model":rawData}
I tried but not working... if I remove JSON.stringify my control not pass to the controller
|
0

You don't need to stringfy your json data.

You can replace this:

JSON.stringify({ "tm": sendingvalue,"tcval":customers}),

to this:

{tm: sendingvalue, tcval: customers}),

2 Comments

if I do so my control not pass to the controller :(
Do you look the difference between each approach on request tab of developers tools? Both should send a json on the request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.