【发布时间】:2019-12-31 01:52:42
【问题描述】:
我有一个带有存储过程的 CRUD,我需要使用剑道网格来处理它。控制器和添加、编辑和删除按钮工作正常,但是在我的 CRUD 中添加一行后,这些按钮(除了添加)不起作用。我的意思是,在我添加之后,所有的按钮都开始只是添加,即使我按下编辑或删除按钮,我也不知道出了什么问题。
我的控制器:
private Entities db = new Entities();
ClientDB cliDB = new ClientDB();
// GET: Client
public ActionResult ClientIndex()
{
return View("ClientIndex");
}
public ActionResult ClientRead([DataSourceRequest]DataSourceRequest request)
{
return Json(cliDB.ListAll().ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ClientCreate([DataSourceRequest]DataSourceRequest request, t_Client t_Client)
{
return Json(cliDB.Add(t_Client), JsonRequestBehavior.AllowGet);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ClientUpdate([DataSourceRequest]DataSourceRequest request, t_Client t_Client)
{
return Json(cliDB.Update(t_Client), JsonRequestBehavior.AllowGet);
}
public ActionResult ClientDelete([DataSourceRequest] DataSourceRequest request, string c_Id)
{
Guid g = Guid.Parse(c_Id);
return Json(cliDB.Delete(g), JsonRequestBehavior.AllowGet);
}
我调用存储过程的类:
public class ClientDB
{
private SqlConnection con;
private void connection()
{
string cs = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;
con = new SqlConnection(cs);
}
public List<t_Client> ListAll()
{
connection();
List<t_Client> lst = new List<t_Client>();
SqlCommand com = new SqlCommand("SP_SelectClient", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
con.Close();
lst = (from DataRow rdr in dt.Rows
select new t_Client()
{
c_Id = (Guid)rdr["c_Id"],
c_Name = rdr["c_Name"].ToString(),
c_NoClients = rdr["c_NoClients"].ToString(),
c_DocType = rdr["c_DocType"].ToString(),
c_DocNumber = rdr["c_DocNumber"].ToString(),
c_TaxpayerType = rdr["c_TaxpayerType"].ToString(),
c_DianClassification = rdr["c_DianClassification"].ToString(),
c_Address = rdr["c_Address"].ToString(),
c_AddrType = rdr["c_AddrType"].ToString(),
c_Telephone = rdr["c_Telephone"].ToString(),
c_BusinessName = rdr["c_BusinessName"].ToString()
}).ToList();
return lst;
}
public int Add(t_Client client)
{
connection();
SqlCommand com = new SqlCommand("SP_InsertUpdateClient", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@c_Id", client.c_Id);
com.Parameters.AddWithValue("@c_NoClients", client.c_NoClients);
com.Parameters.AddWithValue("@c_Name", client.c_Name);
com.Parameters.AddWithValue("@c_DocType", client.c_DocType);
com.Parameters.AddWithValue("@c_DocNumber", client.c_DocNumber);
com.Parameters.AddWithValue("@c_TaxpayerType", client.c_TaxpayerType);
com.Parameters.AddWithValue("@c_DianClassification", client.c_DianClassification);
com.Parameters.AddWithValue("@c_Address", client.c_Address);
com.Parameters.AddWithValue("@c_AddrType", client.c_AddrType);
com.Parameters.AddWithValue("@c_Telephone", client.c_Telephone);
com.Parameters.AddWithValue("@c_BusinessName", client.c_BusinessName);
com.Parameters.AddWithValue("@Action", "Insert");
int i;
con.Open();
i = com.ExecuteNonQuery();
con.Close();
return i;
}
public int Update(t_Client client)
{
connection();
int i;
SqlCommand com = new SqlCommand("SP_InsertUpdateClient", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@c_Id", client.c_Id);
com.Parameters.AddWithValue("@c_NoClients", client.c_NoClients);
com.Parameters.AddWithValue("@c_Name", client.c_Name);
com.Parameters.AddWithValue("@c_DocType", client.c_DocType);
com.Parameters.AddWithValue("@c_DocNumber", client.c_DocNumber);
com.Parameters.AddWithValue("@c_TaxpayerType", client.c_TaxpayerType);
com.Parameters.AddWithValue("@c_DianClassification", client.c_DianClassification);
com.Parameters.AddWithValue("@c_Address", client.c_Address);
com.Parameters.AddWithValue("@c_AddrType", client.c_AddrType);
com.Parameters.AddWithValue("@c_Telephone", client.c_Telephone);
com.Parameters.AddWithValue("@c_BusinessName", client.c_BusinessName);
com.Parameters.AddWithValue("@Action", "Update");
con.Open();
i = com.ExecuteNonQuery();
con.Close();
return i;
}
public int Delete(Guid c_Id)
{
connection();
int i;
SqlCommand com = new SqlCommand("SP_DeleteClient", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@c_Id", c_Id);
con.Open();
i = com.ExecuteNonQuery();
con.Close();
return i;
}
}
cshtml
@(Html.Kendo().Grid<CRUD5.Models.t_Client>()
.Name("grid")
.HtmlAttributes(new { style = "height:500px; height:800px:" })
.Columns(columns =>
{
columns.Bound(c => c.c_NoClients).Title("Clients").Width("auto");
columns.Bound(c => c.c_Name).Title("Name");
columns.Bound(c => c.c_DocType).Title("Doc Type");
columns.Bound(c => c.c_DocNumber).Title("Doc Number");
columns.Bound(c => c.c_TaxpayerType).Title("Taxpayer Type");
columns.Bound(c => c.c_DianClassification).Title("Dian Classification");
columns.Bound(c => c.c_Address).Title("Address");
columns.Bound(c => c.c_AddrType).Title("AddrType");
columns.Bound(c => c.c_Telephone).Title("Telephone");
columns.Bound(c => c.c_BusinessName).Title("Business Name");
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
})
.ToolBar(toolbar =>
{
toolbar.Create();
})
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.Scrollable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(p => p.c_Id))
.Read(read => read.Action("ClientRead", "Client"))
.Create(create => create.Action("ClientCreate", "Client"))
.Update(update => update.Action("ClientUpdate", "Client"))
.Destroy(destroy => destroy.Action("ClientDelete", "Client"))
)
)
我的存储过程和我的表:
CREATE TABLE t_Client(
c_Id UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
c_NoClients NVARCHAR(100) NULL,
c_Name NVARCHAR(100) NULL,
c_DocType NVARCHAR(100) NULL,
c_DocNumber NVARCHAR(100) NULL,
c_TaxpayerType NVARCHAR(100) NULL,
c_DianClassification NVARCHAR(100) NULL,
c_Address NVARCHAR(100) NULL,
c_AddrType NVARCHAR(100) NULL,
c_Telephone NVARCHAR(100) NULL,
c_BusinessName NVARCHAR(100) NULL,
c_Action NVARCHAR(10) NULL,
c_Estate NVARCHAR(6) NULL,
ValidFrom DATETIME2 GENERATED ALWAYS AS ROW START,
ValidTill DATETIME2 GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTill)
)
WITH
(
SYSTEM_VERSIONING = ON
(
HISTORY_TABLE = dbo.t_ClientHistory,
HISTORY_RETENTION_PERIOD = 10 YEARS
)
)
GO
-------------------------------------------------------------------------------------
--Insert and Update Client
Create Procedure SP_InsertUpdateClient
(
@c_Id uniqueidentifier,
@c_NoClients NVARCHAR(100),
@c_Name NVARCHAR(100),
@c_DocType NVARCHAR(100),
@c_DocNumber NVARCHAR(100),
@c_TaxpayerType NVARCHAR(100),
@c_DianClassification NVARCHAR(100),
@c_Address NVARCHAR(100),
@c_AddrType NVARCHAR(100),
@c_Telephone NVARCHAR(100),
@c_BusinessName NVARCHAR(100),
@Action varchar(10)
)
As
Begin
if @Action='Insert'
Begin
insert into t_Client(c_NoClients, c_Name, c_DocType, c_DocNumber, c_TaxpayerType, c_DianClassification , c_Address , c_AddrType , c_Telephone , c_BusinessName )
values (@c_NoClients, @c_Name, @c_DocType, @c_DocNumber, @c_TaxpayerType, @c_DianClassification , @c_Address , @c_AddrType , @c_Telephone , @c_BusinessName )
End
if @Action='Update'
Begin
update t_Client set
c_NoClients = @c_NoClients,
c_Name = @c_Name,
c_DocType = @c_DocType,
c_DocNumber = @c_DocNumber,
c_TaxpayerType = @c_TaxpayerType,
c_DianClassification = @c_DianClassification,
c_Address = @c_Address,
c_AddrType = @c_AddrType,
c_Telephone = @c_Telephone,
c_BusinessName = @c_BusinessName
where c_Id = @c_Id ;
End
End
-------------------------------------------------------------------------------------
--Delete Employee
Create Procedure SP_DeleteClient
(
@c_Id uniqueidentifier
)
as
Begin
if exists (select * from t_Client where c_Id = @c_Id )
update t_Client set c_Estate = 'false' where c_Id = @c_Id;
update t_Client set c_Action = 'Delete' where c_Id = @c_Id;
End
【问题讨论】:
-
非常完整且易于理解的问题,做得好!作为起点,您应该在所有 CRUD 操作中返回一个
DataSourceResult,请参见本示例中的控制器:demos.telerik.com/aspnet-mvc/grid/editing-popup 您是否在浏览器的控制台中收到任何消息? -
@CarstenFranke 非常感谢您的回复,但它不起作用,因为我正在为 ClientDB 使用“公共 int”,并且它是调用存储过程所必需的,如果我尝试添加 DataSourceResult* 它会生成错误:“”int”不包含“DataSourceResult”的定义,并且最佳扩展方法重载 'QueryableExtensions.ToDataSourceResult (DataTable, DataSourceRequest)' 需要接收类型为“DataTable”的“
-
然后将其包装成一个数组:
return Json(new[] { cliDB.Update(t_Client) }.ToDataSourceResult(request, ModelState));如果Update或任何其他操作返回int,您应该考虑检索更新的对象并将其返回。否则网格无法与您返回的内容正常工作。查看来自 Kendo 的示例,他们发送的内容和收到的内容,看看他们是如何做到的。顺便提一句。对于非 GET 请求,您可以删除JsonRequestBehavior.AllowGet。 -
@CarstenFranke 如果我将它返回到一个数组中,它仍然不起作用,如果我尝试将它作为一个对象发送,就像我做的那样,它不起作用而且我没有知道要做什么。你能告诉我如何发送吗?请。
标签: asp.net-mvc kendo-ui grid telerik crud