Niharika
Niharika

Reputation: 71

what should have to write to get array of array in controller from view in MVC?

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

Upvotes: 1

Views: 97

Answers (2)

Matt Ghafouri
Matt Ghafouri

Reputation: 1577

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

Upvotes: 1

Lutti Coelho
Lutti Coelho

Reputation: 2264

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}),

Upvotes: 0

Related Questions