[英]Posting data to controller using ajax
first of all as a win forms developer I'm don't have much experience in web development. 首先,作为一个成功的表单开发人员,我在Web开发方面没有太多经验。 I'm trying my best to get the hang of it 我正在竭尽所能
I have two tables TOURNAMENTS_M (Master) and TOURNAMENTS_D(detail) with scaffold controllers and view. 我有两个带有脚手架控制器和视图的表TOURNAMENTS_M(主)和TOURNAMENTS_D(详细)。
I have edited Master tables Create/and Edit views so that I'm able to display child records as well 我已经编辑了主表的“创建/编辑”视图,这样我也可以显示子记录
When I press the remove button, I call the following function 当我按下删除按钮时,我调用以下函数
function DeleteDetail(pId) {
const con = confirm("are you sure ?");
if (con === true) {
$.ajax({
url: "/TOURNAMENTS_M/Delete",
type: "POST",
contentType: "application/json; charset=utf-8",
data:
{ id: pId }
})
.done(function (result) {
alert("Succeed");
})
.fail(function (result) {
alert("I failed :'( ");
});
}
}
but all I get is the fail message :( what am I missing here? I really appreciate any help, been trying to make this work for hours 但是我得到的只是失败消息:((我在这里想念的是什么?我非常感谢您的帮助,一直在努力使这项工作工作数小时
PS I'm using mvc core PS我正在使用MVC核心
UPDATE: 更新:
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var tOURNAMENTS_M = await _context.TOURNAMENTS_M
.FirstOrDefaultAsync(m => m.TM_ROWID == id);
if (tOURNAMENTS_M == null)
{
return NotFound();
}
return View(tOURNAMENTS_M);
}
this is how I call the function 这就是我所谓的功能
@if (Model != null)
{
var i = 0;
foreach (var detail in Model.TOURNAMENTS_D)
{
<tr>
<td> <input type='hidden' name="TD_LEVEL" id='@("TD_LEVEL")@i' value="@detail.TD_LEVEL" />@detail.TD_LEVEL </td>
<td> <input type='hidden' name="TD_SB" id='@("TD_SB")@i' value="@detail.TD_SB" />@detail.TD_SB </td>
<td> <input type='hidden' name="TD_BB" id='@("TD_BB")@i' value="@detail.TD_BB" />@detail.TD_BB </td>
<td>
<a asp-action="Edit" asp-route-id="@detail.TD_ROWID">Edit</a> |
<button onclick="DeleteDetail(@detail.TD_ROWID) " class="btn btn-primary">Remove</button>
</td>
</tr>
i++;
}
//"<td><a id ='myRemove' data-itemId='0' class='btn btn-primary'>Remove</a ></td> ";
}
As my suggestion in the comments worked, I will provide a bit more information: 由于我在评论中的建议有效,因此我将提供更多信息:
Since you are using MVC Web Api out of the box, it is a RESTful API and therefore the method types are very important. 由于您是开箱即用地使用MVC Web Api,因此它是RESTful API ,因此方法类型非常重要。
You are trying to perform a delete action which requires a 'DELETE' type of request according the RESTful API guidelines. 您正在尝试执行删除操作,该操作需要根据RESTful API准则进行“删除”类型的请求。 MVC Web Api maps this method to the Delete
method out of the box without making use of the [HttpDelete]
attribute. MVC Web Api无需使用[HttpDelete]
属性即可将此方法直接映射到Delete
方法。
Overall, simply change the method type of your ajax call to 'DELETE': 总体而言,只需将您的ajax调用的方法类型更改为“ DELETE”:
function DeleteDetail(pId) {
const con = confirm("are you sure ?");
if (con === true) {
$.ajax({
...
type: "DELETE",
...
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.