简体   繁体   English

CSHTML确认删除,然后再删除数据库记录

[英]CSHTML Confirm Delete prior to deleting database record

I'm working on updating a form for our website. 我正在为我们的网站更新表格。 At the beginning of the code I have an @{} code block that runs updates on a database. 在代码的开头,我有一个@ {}代码块,用于在数据库上运行更新。

I now need to be able to delete a record from that database when a Cancel button is clicked. 现在,单击“取消”按钮后,我需要能够从该数据库中删除一条记录。 Prior to doing that I need to confirm that the user actually wants to delete the record they have started. 在此之前,我需要确认用户确实要删除他们已经开始的记录。

In C# I could use a MessageBox or DialogBox with an OK and CANCEL option, but what can I use inside the @{} code to do the same thing? 在C#中,我可以使用带有OK和CANCEL选项的MessageBox或DialogBox,但是在@ {}代码中可以使用什么做同样的事情? I'm afraid that if I use JavaScript that the record will still be deleted if the user clicks the Cancel button. 恐怕如果我使用JavaScript,如果用户单击“取消”按钮,该记录仍将被删除。

Here is my Cancel button code: 这是我的“取消”按钮代码:

if(!Request["btnCancel"].IsEmpty()){
    var deleteCommand = "DELETE FROM [dbo] WHERE ID=@0";
    db.Execute(deleteCommand, Userid);
    Response.Redirect("~/");
}

Where dbo is the name of my database. 其中dbo是我的数据库的名称。

Thanks to @Shyju for tipping me off to the answer. 感谢@Shyju给我提示的答案。 It was much more simple than I had anticipated. 这比我预期的要简单得多。 I was using an input tag, and luckily, it does have a built in onclick="" attribute. 我使用的是输入代码,幸运的是,它确实具有内置的onclick =“”属性。 I simply did the following: 我只是做了以下事情:

<input type="submit" name="btnCancel"
    class="btn btn-default btn-large"
    value="Cancel Application"
    formnovalidate 
    onclick="return confirm('Are you sure you want to cancel your application?\n\nThis process cannot be undone, and any information you have entered will be lost.');"/>

Now when the button is clicked I get a prompt with an OK or CANCEL button. 现在,单击该按钮时,我将获得一个带有OK或CANCEL按钮的提示。 The OK button continues the posting action while the CANCEL button returns to the page without posting or modifying the page and it's contents. “确定”按钮继续发布操作,而“取消”按钮返回到页面而无需发布或修改页面及其内容。

I hope this helps someone else. 我希望这可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM