您好我从存储过程返回一个数据表,用于绑定网格。该数据表未返回“身份”字段。在这种情况下,请帮助我解雇'更新''销毁'并且'创建'。
这是我的控制器方法,
public JsonResult Employee_Read([DataSourceRequest]DataSourceRequest request)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["manualconn"].ConnectionString))
{
var command = new SqlCommand("usp_FetchUserDetails", con);
command.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return Json(rows, JsonRequestBehavior.AllowGet);
}
// }
}
这是我观点的一部分:
<script type="text/javascript">
var grid = $("#grid").data("kendoGrid");
var Employee = kendo.data.Model.define({
id: "userDetailsId",
fields: {
"userDetailsId": { type: "number" },
"Name": { type: "string" },
"Department": { type: "string" },
"Role": { type: "string" },
"Email": { type: "string" },
}
});
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: '@Url.Action("Employee_Read", "UserSummary")',
dataType: "json",
cache: false,
type: 'GET',
//data: {
// test: $("#Names").val()
//}
},
destroy:
//function (e) {
{
url: '@Url.Action("Update_Details", "UserSummary")',
type: "POST",
// dataType: "json",
//data: {
// DAKy: $("#Names").val(),
// DIKy: $("#btntxt").val()
//}
},
create: {
url: '@Url.Action("Update_Details", "UserSummary")',
type: "POST",
// dataType: "json",
//cache: false,
//data: {
// AKy: $("#Names").val(),
// IKy: $("#btntxt").val()
//}
},
update:
{
url: '@Url.Action("Update_Details", "UserSummary")',
type : "POST"
//data: {
// AKy: $("#Names").val(),
// IKy: $("#btntxt").val()
// }
}
},
error: function (e) {
// handle error
alert("Status: " + e.status + "\n" + e.errorThrown);
},
pageSize: 5,
schema: {
model: {
id: "userDetailsId",
model: Employee
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
editable: "inline",
//toolbar: ["create", "save"],
autobind: true,
pageable: true,
columns: [
//{
// field: "userDetailsId",
// title: "userDetailsId",
// width: "50px",
//},
{
field: "Name",
title: "Name",
width: "75px",
template: "<input id='Name' type='text' value='#: Name #' readonly> </input>",
editable:false,
},
{
field: "Department",
title: "Department",
width: "50px",
editor: ddlFetchDepartments
},
{
field: "Role",
title: "Role",
width: "50px",
editor: ddlFetchRoles
},
{
field: "Email",
title: "Email",
width: "100px",
template: "<input type='text' id='Email' size='35' value='#:Email#' readonly> </input>",
editable:false,
},
{
command: ["edit", "destroy"], title: " ", width: "75px"
},
{ command: { text: "custom edit", click: showDetails },title: " ", width: "60px" }
],
});
function showDetails(e) {
e.preventDefault();
//debugger;
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
alert("View Details\n Name : " + dataItem.Name+"\nuserDetailsId : "+dataItem.userDetailsId);
@{
// ((MockProject.Controllers.UserSummaryController)this.ViewContext.Controller).Update_Details();
}
}
function ddlFetchDepartments(container, options) {
$('<input name="Departments" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
dataTextField: "deptName",
dataValueField: "deptId",
autoBind: false,
dataSource: new kendo.data.DataSource({
transport: {
read: {
url: '@Url.Action("FetchDepartments", "UserSummary")',
type: 'GET',
dataType: "json"
},
schema: {
model: {
id: "deptId",
value: "deptName"
}
}
}
})
});
}
function ddlFetchRoles(container, options) {
$('<input name="Roles" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
dataTextField: "roleName",
dataValueField: "roleId",
autoBind: false,
dataSource: new kendo.data.DataSource({
transport: {
read: {
url: '@Url.Action("FetchRoles", "UserSummary")',
type: 'GET',
dataType: "json"
},
schema: {
model: {
id: "roleId",
value: "roleName"
}
}
}
})
});
}
@*@{
((HomeController)this.ViewContext.Controller).Method1();
}*@
</script>
<br/>
<button type="button" id="btn_addUser" > Add User</button>
<input type="submit" style="visibility:hidden" name="btn_save" class="k-button k-button-icontext" id="btn_save" value="Save" onclick="saveDataToDb()" />
<script type="text/javascript">
function saveDataToDb() {
}
$('#btn_addUser').click(function () {
document.getElementById('btn_save').style.visibility = "visible";
$('#grid').data('kendoGrid').addRow();
});
function onEdit(e) {
//Custom logic to default value
var name = $("#AddSingleSuppliment").text();
// If addition
if (e.model.isNew()) {
//set field
e.model.set("Name", name); // Name: grid field to set
}
}
</script>
<script>
$(document).ready(function () {
$("#btn_addUser").kendoButton({
spriteCssClass: "k-icon iconplus"
});
});
</script>
<style scoped>
.k-button .k-image {
height: 16px;
}
.demo-section {
width: 500px;
}
.iconplus {
background-image: url("../content/icons/plus.png");
background-size: contain;
/*background-position: 0 -64px;
align-content: flex-start;
align-self: auto;*/
}
</style>