【发布时间】:2019-07-11 15:53:42
【问题描述】:
我正在设置一个 asp.net 网络应用项目。现在解决问题: (我是 Ajax、JQuery 和 Asp.net 的新手)
1) 我找到了一种将复选框的 ID 存储在字符串“CommaSeparatedIDs”中的方法 - 在 Index.cshtml 文件中(SaveFinished() 函数)
2) 在“Index.cshtml.cs”中定义“SaveFinished()”函数。我不知道如何定义函数/在文件中定义它的位置。函数的用途:具有在 1) 中定义的字符串参数,它使用 ID 操作数据库。
3) 最后,“SaveFinished()”的实现可能有我无法指出的错误。
我尝试在“Index.cshtml.cs”的不同区域定义函数“SaveFinished()”,但该函数从未被识别或引用,我也不知道该函数的类型应该是什么。
Index.cshtml:
<table class="table" id="MainTable"> // (Table used to define the various entries)
...
...
<th>
<a class="btn btn-success" onclick=SaveFinished()>Done</a> // JS function SaveFinished()
</th>
...
...
@foreach (var item in Model.ExcelData) // ExcelData is the object defined in Index.cshtml.cs (given below)
<tr>
<td>
<input id="@item.ID" type="checkbox" />
</td>
...
...
</tr>
// I think I have errors below which I can't figure out.
<script>
var SaveFinished = function () {
var ArrItem = [];
var CommaSeparatedIDs = "";
$("MainTable tbody tr input[type=checkbox] ").each(function () {
debugger
var checkId = $(val).attr("id");
var IsChecked = $("#" + checkId).is(":checked", true);
if (IsChecked) {
ArrItem.push(checkId);
}
})
if (ArrItem.length != 0) {
CommaSeparatedIDs = ArrItem.toString();
$.ajax({
url: "/Excel_Data/Index", <!--This should be the URL of the page?-->
type: "POST",
data: { MainTable: CommaSeparatedIDs },
success: function (response) {
<!-- What should I do here? I'm new to Ajax and JQuery -->
}
})
}
}
</script>
索引.cshtml.cs:
namespace Customer.Pages.Excel_Data
{
public class IndexModel : PageModel
{
private readonly Customer.Models.CustomerContext _context;
public IndexModel(Customer.Models.CustomerContext context)
{
_context = context;
}
#region Variables
public async Task OnGetAsync()
{
#region Data manipulation and setting variables
}
}
}
我的预期结果是将“SaveFinished()”函数中的字符串“CommaSeparatedIDs”传递给页面的逻辑部分——“Index.cshtml.cs”并操作数据库。
【问题讨论】:
-
你的模型和 Index.cshtml.cs 中的 post 处理程序的定义是什么?
标签: c# asp.net ajax asp.net-mvc asp.net-core