要从MVC的视图中获取控制器中的数组,应该写些什么?

时间:2019-12-07 07:31:20

标签: jquery asp.net-mvc-4

我正在使用asp.net mvc4 我在jquery中创建未传递给控制器​​的数组数组 这是我的jQuery代码

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

这是我的ajax电话

$.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.'); }
                    });

这是我的控制器代码

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

这是我的对象

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; }
}

从ajax传递数组,但不在控制器端。在这里,我上传了ajax传递的值和控制器获取的值

AjaxPassed Value Conroller Get Value

此处是控制台中的JSON响应

JSON Response Imnage

任何人都可以找到解决方案,然后请帮助我。预先谢谢你

2 个答案:

答案 0 :(得分:0)

我认为您需要定义一个像这样的复杂对象:

namespace Tank
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            this.Text = Properties.Settings.Default.Tankhöhe.ToString();

        }

        private void btnLoadHeight_Click(object sender, EventArgs e)
        {

                Properties.Settings.Default.Tankhöhe = Convert.ToDouble(mtbTankhöhe.Text);


        }

MyArray对象应该像这样:

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

这是您的操作:

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

 }

还有您的JSON设置数据部分:

[HttpPost]
Public ActionResult GenerateTC(MyModel model)

。 。 。

我再次检查了代码,最后通过更改以下部分在操作中获得了价值:

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

现在,要将参数值转换为模型,您需要使用特定的 ModelBinder 或手动执行此特定操作

答案 1 :(得分:0)

您不需要对您的json数据进行字符串化处理。

您可以替换为:

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

对此:

{tm: sendingvalue, tcval: customers}),