我在控制器中创建了一个动作,该视图需要String
参数。在视图中,我有一个超链接和onclick
事件,我想调用jQuery
函数将该值发送给操作。
$(function () {
debugger;
$('.delete-icon').click(function () {
//alert($(this.id));
var answer = confirm('Do you want to remove this image?');
var id1 = $('.delete-icon').data('val1');
var jsonval = JSON.stringify(id1);
alert(jsonval);
if (answer) {
$.ajax({
type: 'POST',
contentType: "application/json;charset=utf-8",
//data: { id: jsonval },
//url:'@Url.Action("ImageDelete", "AdminNew")',
url: '/AdminNew/ImageDelete/' + jsonval,
dataType: 'json',
//traditional: true,
success: function (ajaxresult) {
$("#" + jsonval).remove();
},
failure: function (ajaxresult, status) {
console.log(ajaxresult)
}
});
return false;
}
return false;
});
});
<table>
<% foreach (var imagealbum in ViewBag.ImageList)
{ %>
<tr id="<%:imagealbum.frame_image_id %>">
<td class="checkbox">
<input id="chk" type="checkbox" checked="checked" /></td>
<td class="image">
<img src="<%:imagealbum.image_path%>" width="50" height="50" />
</td>
<td class="image-name"><%:imagealbum.image_path%> </td>
<td class="image-delete">
<a href="<%:Url.Action("ImageDelete", "AdminNew", new { id = imagealbum.frame_image_id})%>" class="delete-icon" data-val1="<%:imagealbum.frame_image_id%>"></a>
</td>
</tr>
<%} %>
</table>
答案 0 :(得分:0)
您不需要传递额外的值,因为所需的值已经在链接的href中。
$(function () {
$('.delete-icon').click(function (e) {
e.preventDefault();
var answer = confirm('Do you want to remove this image?');
var deleteIcon = $(this);
var url = deleteIcon.prop('href');
if (answer) {
$.ajax({
type: 'POST',
contentType: "application/json;charset=utf-8",
url: url,
dataType: 'json',
success: function (ajaxresult) {
deleteIcon.closest('tr').remove();
},
failure: function (ajaxresult, status) {
console.log(ajaxresult)
}
});
}
});
});
答案 1 :(得分:0)
尝试以下
public ActionResult SendStream(string path)
{
}
$.ajax({
url: url, //Without appending Json value
data: JSON.stringify({ path: "Some Val" }), //Parameters
type: 'POST',
contentType: 'application/json, charset=utf-8',
dataType: 'json'
}).done(function (data) { //Success Callback
}).fail(function (data) { //Failed Callback
}).always(function(data) { //Request completed Callback
});
答案 2 :(得分:0)
首先在下面创建一个ActionResult。
public ActionResult SendStream(string path){
}
现在,
$(function () {
$('.delete-icon').click(function () {
var answer = confirm('Do you want to remove this image?');
var id1 = $('.delete-icon').data('val1');
var jsonval = JSON.stringify(id1);
if (answer) {
$.ajax({
type: 'POST',
contentType: "application/json;charset=utf-8",
url: '/Shared/SendStream?path=' + jsonval,
dataType: 'json',
success: function (ajaxresult) {
$("#" + jsonval).remove();
},
failure: function (ajaxresult, status) {
console.log(ajaxresult)
}
});
return false;
}
return false;
});
});
请尝试上面的示例并检查。