Jestem nowy do JSON DataType. Jak go pobrać .. Spójrz na poniższy kod.

To jest mój kod JavaScript:

function fnDeleteSelected() {
            var count_checked = $("[name = 'myChkBox[]']:checked").length;            
            var arrayOfID = [];

            $(':[name = "myChkBox[]"]:checked').each(function () {
                arrayOfID.push($(this).val());
            });            
            var test = JSON.stringify(arrayOfID);
            alert(test);
            if (count_checked == 0) {
                alert("Please Select a Student to delete");
                return false;
            }
            else {
                var confirmDel = confirm("Are you sure you want to delete this?");                
                if (confirmDel == true) {
                    jQuery.ajax({
                        url: baseUrl + "DeleteSelected/",
                        type: 'Post',
                        dataType: 'Json',
                        data: { Parameters: test },
                        success: function (msg) {
                            jQuery("input:checkbox:checked").parents("tr").remove();
                        }
                    });enter code here
                }
            }
        }

Tutaj wysyłanie danych do kontrolera to parametry, w których parametry = ["143", "144", "145"]

A mój kontroler jest: gdzie parametry są przekazywane jako "[" 143 "," 144 ",

public JsonResult DeleteSelected(string [] Parameters)
        {string strConn = "Data Source=localhost;Initial Catalog=Information;Integrated Security=SSPI;";
        SqlConnection conn = new SqlConnection(strConn);

        string strSql = "DELETE FROM dbStudent where ID in";

        SqlCommand myCommand = new SqlCommand(strSql, conn);
        try
        {
            conn.Open();
            myCommand.ExecuteNonQuery();
            conn.Close();                
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return Json(Parameters, JsonRequestBehavior.AllowGet);
    }

Co powinno być w Strsql .. ??

0
user3064407 6 grudzień 2013, 10:52

2 odpowiedzi

Najlepsza odpowiedź

Możesz zmienić swój post AJAX, aby był taki: (ContentTittype jest ważny)

  jQuery.ajax({
                url: baseUrl + "DeleteSelected/",
                type: 'Post',
                dataType: 'json',
                data: JSON.stringify(arrayOfID),
                contentType: 'application/json; charset=utf-8',
                success: function (msg) {
                    jQuery("input:checkbox:checked").parents("tr").remove();
                }
            });

I twoja metoda działania

  public JsonResult DeleteSelected(int[] Parameters)
    {
        string strConn = "Data Source=localhost;Initial Catalog=Information;Integrated Security=SSPI;";
        SqlConnection conn = new SqlConnection(strConn);

        var strSql = "DELETE FROM dbStudent where ID IN (" + String.Join(",", Parameters) + ")";

        SqlCommand myCommand = new SqlCommand(strSql, conn);
        try
        {
            conn.Open();
            myCommand.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return Json(Parameters, JsonRequestBehavior.AllowGet);
    }
0
Ahmad Ibrahim 6 grudzień 2013, 13:06

Możesz konwertować tablicę do tablicy int

int[] myInts = Parameters.Select(int.Parse).ToArray();

Następnie

var query = "DELETE FROM dbStudent where ID IN (" +
    String.Join(",", myInts ) + ")";
0
Nitin Varpe 6 grudzień 2013, 07:02