我正在開發使用ASP.Net mvc 4.我在我的索引視圖中有刪除功能。之前用戶進行刪除一個項目,這是步驟要經過如何從javascript的動作調用打開查看結果
彈出一個確認對話框,以獲得「是」或「不」使用javascript回答。
如果用戶說「是」,我所說的刪除操作從我的控制器
刪除操作從數據庫中刪除項目
刪除操作返回「RedirectToAction(‘指數’) ;」
..假設的觀點將與最新的更新進行更新
第五步是我的問題..它不工作
這裏是我的代碼
刪除按鈕
<a href="@Url.Action("DeleteFile", new { id = @item.Id })"
onclick = "return confirm2delete(this);"
class="btn btn-danger btn-sm"
data-toggle="tooltip" title="Delete File">
<i class="fa fa-trash-o"></i>
</a>
javascript
function confirm2delete(obj)
{
deleteLinkObj = $(this);
w2confirm("Are sure to delete this file ?")
.yes(function() {
$.get(obj.href, function (data) {
alert(data); // i checked ..this return the html page created from the delete action
window.open (data); // not the right approach ???
});
})
.no(function() {
alert("no");
result = false;
});
return false;
}
在我的控制器
public ActionResult DeleteFile(int id)
{
FileUpload f = db.FileUploads.Find(id);
try
{
FileInfo dfile = new FileInfo(f.fileUrl);
if (dfile.Exists) { dfile.Delete(); }
fileUrl = f.FileUrl.Replace("Images/Uploads", "Images/Thumbnails");
FileInfo thumbnail= new FileInfo(fileUrl);
if (thumbnail.Exists) { thumbnail.Delete(); }
db.FileUploads.Remove(f);
db.SaveChanges();
}
catch (Exception ex)
{
}
return RedirectToAction("Index"); // or may i should return something else to make it work in javascript ???
}
刪除操作希望你們能幫助我。我一直在尋找和嘗試幾天來達到這個水平,我覺得它的時間得到一些幫助。再多一點。差不多了。
首先ajax調用保持在同一頁面,所以'返回RedirectToAction(「索引」);'是毫無意義的。最簡單的方法是返回一個json值,指示項目已成功刪除,然後從DOM中刪除相應的項目。 –
你需要顯示你的行之一的HTML來解決這個 –