将视图数据发布到MVC中的控制器,该控制器由javascript数据和模型数据组成。

时间:2017-12-09 19:56:49

标签: javascript c# ajax asp.net-mvc

我正在尝试将视图数据发布到控制器

索引视图 -

@using (Html.BeginForm("GetDataNow", "Home", new { onsubmit = "return 
GetTableData() " }, FormMethod.Post))
        {
            <script>
                //Create a Pass Array List
                var CellValuePassArray = [];
                function GetTableData() {

                    //gets table
                    var GetTableDataCollection = 
 document.getElementById('myTableData');
                    //gets rows of table
                    var GetRowDataCollection = 
 GetTableDataCollection.rows.length;
                    //loops through rows    
                    for (LoopRowCounter = 0; LoopRowCounter < 
 GetRowDataCollection; LoopRowCounter++) {
                        //gets cells of current row  
                        var GetCellsInDataRow = 
 GetTableDataCollection.rows.item(LoopRowCounter).cells;
                        //gets amount of cells of current row
                        var GetTotalCellCountInRow = 
 GetCellsInDataRow.length;
                        //loops through each cell in current row
                        for (var LoopCellCounter = 0; LoopCellCounter < 
 GetTotalCellCountInRow; LoopCellCounter++) {
                            // get your cell info here
                            var cellVal = 
 GetCellsInDataRow.item(LoopCellCounter).innerHTML;
                            if ((LoopCellCounter > 0) && (LoopRowCounter > 
 0) &&(LoopCellCounter < 5)) {

                                if (LoopCellCounter == 1) {
                                    CellValuePassArray.push(cellVal);
                                }
                                if (LoopCellCounter == 2) {
                                    CellValuePassArray.push(cellVal);
                                }
                                if (LoopCellCounter == 3) {
                                    CellValuePassArray.push(cellVal);
                                }
                                if (LoopCellCounter == 4) {
                                    CellValuePassArray.push(cellVal);
                                }
                            }
                        }
                    }

                    //Post using ajax
                    $.ajax({
                        type: "POST",
                        url: "@Url.Action("GetDataNow")",
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(CellValuePassArray),
                        success: function (data) {},
                        failure: function (errMsg) { alert(errMsg); }
                    });
                  //  alert("Data Sent To DB ");
                    return true;
                }
            </script>

           @Html.Label("First Name")
           @Html.TextBoxFor(m => m.CustomerDataModel.FirstName)

//控制器操作结果

    [HttpPost]
    public ActionResult GetDataNow( StoneCraftViewModel CDM, List<string> 
           FieldTemplateDetails)
    {

        //Call Template Order Model Assignemnt
        string name = "";

        name = CDM.CustomerDataModel.FirstName;

      // Do some processing outside the controller

        return RedirectToAction("Index");
    }

在控制器中,FieldTemplateDetails中应该有真实的表数据 并且CDM应该具有名字的用户输入数据。

我得到FieldTemplateDetails数据,但CDM为空。如果我删除GetTable数据并只是发布表单数据,CDM将包含名字。 enter image description here

0 个答案:

没有答案