Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how send table content to controller

I have a problem to send table global from view to controller the table in controller is full but in controller affect a null for the composant of table and this is the controller method :

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

And this the view and method ajax how i append my table global and how i sent this table global from view to controller :

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;

    }

and when i added the length it send him correctly to controller.

like image 816
darkrez Avatar asked Sep 08 '26 09:09

darkrez


2 Answers

but method ion your controller like this: public Boolean ajoutermodule(string nom, stirng s, int cv) { return true; }

and add this to your method 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");
                }
            }
        });

    }

it will work inchallah

like image 67
razzek Avatar answered Sep 10 '26 23:09

razzek


Please try this code for 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>

And then sends the data to controller

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

Please refer the site for more details
https://alexkuznetsov.wordpress.com/2013/05/08/asp-net-mvc-pass-dictionary-data-from-view-to-controller/

like image 26
Shibu Thomas Avatar answered Sep 11 '26 00:09

Shibu Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!