如何将表内容发送到控制器

时间:2016-01-08 11:47:08

标签: javascript asp.net

我有一个问题是将表全局从视图发送到控制器控制器中的表已满但在控制器中会影响表的组合 这是控制器方法:

public Boolean ajoutermodule(string nom, modules[] global, int cv)
{
    return true;
}

这个视图和方法ajax我如何将我的表追加到全局以及我如何将这个表从视图发送到控制器:

function Addmodule() {
        var nom = $("#nomprojet_I").val();
        var cv = global.length;
        $.ajax({
            url: "/Module/ajoutermodule",
            type: "POST",
            dataType: 'json',
            data: {
                "nom": nom,
                "global": global,
                "cv": cv,
            },
            success: function (responseText) {
                debugger;
                if (responseText == "True") {
                    alert("Succes");
                }
                else {
                    alert("error");
                }
            }
        });

    }

    var global = [];
    function OnGetSelectedFieldValues(s, e) {
        var SelectedUsers = $("#teamlist_I").val() + "    " + $("#teamid_I").val();
        listbox.AddItem(SelectedUsers);


        var nom = $("#teamlist_I").val();

        var id = $("#teamid_I").val();
        global.push({ "id": id, "nom": nom });
        debugger;

    }

当我添加长度时,它将他正确地发送给控制器。

2 个答案:

答案 0 :(得分:1)

请尝试使用ASP.NET MVC的代码 -

<强> View.cshtml

<table id="StepsTable">
<tr>
    <td>Step 1</td>
    <td>@Html.TextBox("step1")</td>
</tr>
<tr>
    <td>Step 2</td>
    <td>@Html.TextBox("step2")</td>
</tr>
<tr>
    <td>Step 3</td>
    <td>@Html.TextBox("step3")</td>
</tr>
</table>
<input id="SendToControllerButton" type="button" value="Send to the server"/>
<script>
    $(document).ready(function () {

        $("#SendToControllerButton").click(function () {

            var data = {};
            //Collects the data from textboxes and adds it to the dictionary
            $("#StepsTable tr").each(function (index, item) {
                var tds = $(this).find("td");
                var textBoxTitle = $(tds).eq(0).text();
                var textboxValue = $(tds).eq(1).find("input").val();
                data["stepsDictionary[" + index + "].Key"] = textBoxTitle;
                data["stepsDictionary[" + index + "].Value"] = textboxValue;
            });

            //Makes ajax call to controller 
            $.ajax({
                type: "POST",
                data: data,
                url: "/Home/ProcessStepsValues",
                success: function (message) {
                    alert(message);
                }
            });

        });

    });
</script>

然后将数据发送到控制器

<强> Controller.cs

   [HttpPost]
   public string ProcessStepsValues(Dictionary<string, string> stepsDictionary)
       {
           string resultMessage = string.Empty;
           if (stepsDictionary != null)
           {
               resultMessage = "Dictionary data passes to controller successfully!";
           }
           else
           {
               resultMessage = "Something goes wrong, dictionary is NULL!";
           }
           return resultMessage;
       }

请参阅网站了解更多详情 https://alexkuznetsov.wordpress.com/2013/05/08/asp-net-mvc-pass-dictionary-data-from-view-to-controller/

答案 1 :(得分:1)

但是像你这样控制你的控制器:  public Boolean ajoutermodule(string nom,stirng s,int cv)     {         返回true;     }

并将其添加到您的方法ajax

var s = JSON.stringify(global);

function Addmodule() {
        var nom = $("#nomprojet_I").val();
var s = JSON.stringify(global);
        var cv = global.length;
        $.ajax({
            url: "/Module/ajoutermodule",
            type: "POST",
            dataType: 'json',
            data: {
                "nom": nom,
                "s": s,
                "cv": cv,
            },
            success: function (responseText) {
                debugger;
                if (responseText == "True") {
                    alert("Succes");
                }
                else {
                    alert("error");
                }
            }
        });

    }

它将适用于inchallah