[英]Datatable Editor to work with asp.net webforms
我正在从应用程序开发一个asp.net Web,同时遵循了编辑器网络软件包文档和WebForms / ASPX tuto
服务器端代码是
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void GetTableResultsUsingEditorNet()
{
var db = new Database("sqlserver", SolutionWideSettings.ConnectionString.xxx);
var request = HttpContext.Current.Request;
using (db)
{
var response = new Editor(db, "VacationCalendar", "VacationCalendarID")
.Model<VacationCalendar>()
.Field(new Field("CalendarName_EN")
.Validator(Validation.NotEmpty())
)
.Field(new Field("CalendarName_FR")
.Validator(Validation.NotEmpty())
)
.Field(new Field("Active")
.SetFormatter((val, data) => (string)val == "" ? 0 : 1)
)
.Process(request)
.Data();
string finaljson = JsonConvert.SerializeObject(response);
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write(finaljson);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
客户端代码是
VacationCalendarEditor = new $.fn.dataTable.Editor({
ajax: {
url: "<%= System.IO.Path.GetFileName(Request.Url.AbsolutePath) %>/GetTableResultsUsingEditorNet",
type: 'POST'
},
table: "#VacationCalendarTableUsingEditorNet",
idSrc: "DT_RowId",
fields: [{
label: "Name En",
name: "CalendarName_EN"
},
{
label: "Name Fr",
name: "CalendarName_FR"
},
{
label: "Active",
name: "Active",
type: "checkbox",
options: [
{ "label": "", value: 1 }
],
separator: '',
unselectedValue: 0
}
]
});
$('table#VacationCalendarTableUsingEditorNet').DataTable({
"serverSide": true,
"deferLoading": 1,
dom: "Bfrtip",
idSrc: "DT_RowId",
ajax: {
"url": "<%= System.IO.Path.GetFileName(Request.Url.AbsolutePath) %>/GetTableResultsUsingEditorNet",
"type": "POST",
"dataType": "json",
"contentType": "application/json; charset=utf-8"
columns: [
{ data: "CalendarName_EN" },
{ data: "CalendarName_FR" },
{ data: "Active" }
],
"deferRender": true,
"paging": true,
"pagingType": "full_numbers",
"pageLength": 10,
"ordering": true,
"jQueryUI": false,
"info": true,
"searching": true,
"order": [[0, "asc"]],
responsive: false,
"language": {
"lengthMenu": '<%= RootWebsite.WebSiteTools.Language_GetTwoLetterLang() == "fr" ? "Afficher _MENU_ résultats par page" : "Show _MENU_ entries per page" %>',
"zeroRecords": '<%= GetLocalResourceObject("No results found") %>',
"paginate": {
"first": '<i class="fa fa-angle-double-left"></i>',
"last": '<i class="fa fa-angle-double-right"></i>',
"next": '<i class="fa fa-angle-right"></i>',
"previous": '<i class="fa fa-angle-left"></i>'
},
},
select: true,
buttons: [
{ extend: "create", editor: VacationCalendarEditor },
{ extend: "edit", editor: VacationCalendarEditor },
{ extend: "remove", editor: VacationCalendarEditor }
]
});
我能够加载我的数据表,并可以打开所有编辑器模式来创建,更新或删除,但是我陷于杂乱无章的过程中并得到以下错误:
错误:DataTables警告:表ID = VacationCalendarTableUsingEditorNet-Ajax错误。 有关此错误的更多信息,请参见http://datatables.net/tn/7
我缺少让编辑器插件在Webforms项目上工作的什么功能?
我做了故障排除,我发现我收到了一个双重ajax请求作为响应。 这就是我得到的http:// localhost:48849 / MyProject / GetTableResultsUsingEditorNet / GetTableResultsUsingEditorNet而不是http:// localhost:48849 / MyProject / MyForm.aspx / GetTableResultsUsingEditorNet的原因? 在我的ajax选项中,我只有一个post调用来获取要加载的数据表,就像Webforms / ASPX示例中那样 。 我想念什么?
我得出了一个结论,我可能是错的,但是我认为Editor不适合asp.net Webform,这是我尝试过的。
在我添加的编辑器块中:
ajax: {
create: {
type: 'POST',
url: "<%= System.IO.Path.GetFileName(Request.Url.AbsolutePath) %>/CreateVacationCalendar"
},
edit: {
type: 'PUT',
url: "<%= System.IO.Path.GetFileName(Request.Url.AbsolutePath) %>/UpdateVacationCalendar"
},
remove: {
type: 'DELETE',
url: "<%= System.IO.Path.GetFileName(Request.Url.AbsolutePath) %>/DeleteVacationCalendar"
}
在Datatable块中,我将ajax调用更改为Get而不是Post
ajax: {
"url": "<%= System.IO.Path.GetFileName(Request.Url.AbsolutePath) %>/GetTableResultsUsingEditorNet",
"type": "GET",
"dataType": "json",
"contentType": "application/json; charset=utf-8"
在后面的代码(aspx.cs文件)中,我针对那些CRUD请求执行了每种方法,唯一可以使用的方法就是Get方法。
[WebMethod]
[HttpGet]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static void GetTableResultsUsingEditorNet()
{
HERE THE CODE TO GET WHICH RETURN DATA. ITS THE SAME AS THE ONE IN THE Webforms/ ASPX example.
}
对于创建过程,我执行了以下操作
[WebMethod]
[HttpPost]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void CreateVacationCalendar()
{
HERE THE CODE WHICH IS THE SAME AS THE GetTableResultsUsingEditorNet()
}
在Visual Studio调试(如Get)中甚至没有标记出最后一个。 Ajax响应为空。 在此调用之后,Get调用完成了,但是返回了404错误,并带有以下响应URL: http:// localhost:48849 / MyProject / CreateVacationCalendar / GetTableResultsUsingEditorNet?_ = 1568222333345 。
这就是为什么我认为编辑器无法与WebForms一起使用的原因,因为看起来这最后一个URL是mvc逻辑,其中指定了Controller以及方法名称和参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.