如何使用mvc3从数据库中删除数据?

时间:2013-12-06 06:52:02

标签: javascript json asp.net-mvc-3 delete-row

我是Json数据类型的新手。如何回顾它..请看下面的代码。

这是我的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
                }
            }
        }

这里发送给控制器的数据是参数,其中参数= [“143”,“144”,“145”]

和我的控制器是:其中参数传递为“[\”143 \“,\”144 \“,\”145 \“]”我的问题是如何解析参数,以便它可以嵌入在sql中声明

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

strSql中应该有什么.. ??

2 个答案:

答案 0 :(得分:0)

您可以将数组转换为int数组

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

然后

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

答案 1 :(得分:0)

您可以将您的ajax帖子更改为:( contentType很重要)

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

和你的行动方法一样

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