C# 未通过ajax保留的新行通过json绑定,但在数据库中
我在数据表上使用Jquery,但它不保留新行或回车,但数据库中的值很好,当我将它们复制到SSMS中时,它会在其文本编辑器中正确显示它们C# 未通过ajax保留的新行通过json绑定,但在数据库中,c#,jquery,asp.net-core,C#,Jquery,Asp.net Core,我在数据表上使用Jquery,但它不保留新行或回车,但数据库中的值很好,当我将它们复制到SSMS中时,它会在其文本编辑器中正确显示它们 <script> $(document).ready(function () { var id = @ViewBag.CaseId; $("#audTrailTable").DataTable({ "ajax": { url: "/MISObj
<script>
$(document).ready(function () {
var id = @ViewBag.CaseId;
$("#audTrailTable").DataTable({
"ajax": {
url: "/MISObjects/GetAuditTrailData/" + id,
type: "get",
database: "json"
},
"columns": [
{ "data": "createdDate" },
{ "data": "createdBy" },
{ "data": "action" }
],
"scrollY": "200px",
"scrollCollapse": true,
"paging": false,
"processing": true, // for show progress bar
"filter": true, // this is for disable filter (search box)
"orderMulti": false // for disable multiple column at once
})
});
</script>
$(文档).ready(函数(){
var id=@ViewBag.CaseId;
$(“#audTrailTable”).DataTable({
“ajax”:{
url:“/MISObjects/GetAuditTrailData/”+id,
键入:“获取”,
数据库:“json”
},
“栏目”:[
{“数据”:“createdDate”},
{“数据”:“createdBy”},
{“数据”:“操作”}
],
“滚动”:“200px”,
“卷轴崩溃”:没错,
“分页”:false,
“处理”:true,//用于显示进度条
“过滤器”:true,//这是用于禁用过滤器(搜索框)
“orderMulti”:false//用于一次禁用多个列
})
});
我的Html
<table id="audTrailTable" class="table table-striped table-bordered dt-responsive nowrap">
<thead>
<tr>
<th>Date</th>
<th>User</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
日期
使用者
行动
我的方法
[HttpGet]
public ActionResult<DataTableWrapper<List<MISAuditTrail>>> GetAuditTrailData(int? id) {
int? resulCaseId = id;
var auditTrailsHistory = _context.MisAuditTrail.Where(w => w.isActive == true && w.isDeleted == false && w.MISObjectId == resulCaseId).ToList();
string output = JsonConvert.SerializeObject(auditTrailsHistory);
DataTableWrapper<List<MISAuditTrail>> data = new DataTableWrapper<List<MISAuditTrail>>() {
data = auditTrailsHistory
};
return data;
}
[HttpGet]
公共操作结果GetAuditTrailData(int?id){
int?resultcaseid=id;
var audittrailshhistory=\u context.MisAuditTrail.Where(w=>w.isActive==true&&w.isDeleted==false&&w.MISObjectId==resultcaseid).ToList();
字符串输出=JsonConvert.SerializeObject(auditTrailsHistory);
DataTableWrapper数据=新DataTableWrapper(){
数据=auditTrailsHistory
};
返回数据;
}
要在datatable中显示数据的新行或回车符,我们需要添加render
方法来确定绑定datatable js函数中的列时是否包含换行符或回车符
如果是,请在相应位置添加
,以确保换行效果显示在表格中
<script>
$(document).ready(function () {
var id = @ViewBag.CaseId;
$("#audTrailTable").DataTable({
"ajax": {
url: "/MISObjects/GetAuditTrailData/" + id,
type: "get",
database: "json"
},
"columns": [
{ "data": "createdDate" },
{ "data": "createdBy" },
{
"data": "action",
render: function (data, type, row) {
if (type === "sort" || type === "type") {
return data;
}
var match = /\r|\n/.exec(data);
String.prototype.splice = function (idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
if (match) {
data = data.splice(match.index, 0, "<br />");
}
return data;
}
},
],
"scrollY": "200px",
"scrollCollapse": true,
"paging": false,
"processing": true, // for show progress bar
"filter": true, // this is for disable filter (search box)
"orderMulti": false // for disable multiple column at once
})
});
</script>
$(文档).ready(函数(){
var id=@ViewBag.CaseId;
$(“#audTrailTable”).DataTable({
“ajax”:{
url:“/MISObjects/GetAuditTrailData/”+id,
键入:“获取”,
数据库:“json”
},
“栏目”:[
{“数据”:“createdDate”},
{“数据”:“createdBy”},
{
“数据”:“行动”,
呈现:函数(数据、类型、行){
如果(类型==“排序”| |类型==“类型”){
返回数据;
}
var match=/\r |\n/.exec(数据);
String.prototype.splice=函数(idx、rem、str){
返回this.slice(0,idx)+str+this.slice(idx+Math.abs(rem));
};
如果(匹配){
data=data.splice(match.index,0,“
”);
}
返回数据;
}
},
],
“滚动”:“200px”,
“卷轴崩溃”:没错,
“分页”:false,
“处理”:true,//用于显示进度条
“过滤器”:true,//这是用于禁用过滤器(搜索框)
“orderMulti”:false//用于一次禁用多个列
})
});
以下是测试结果:
这一点以前也曾在一个非常类似的问题中得到过回答。看“他在分裂,这是不一样的,虽然。哇,所有这一切只是一个br似乎超过了杀死mabe时间切换到blazorthank u将尝试一下,我不,这将与你的能力虽然工作。