[英]How to create the confirm box in mvc controller?
我需要在 mvc 控制器中創建確認框嗎?。 使用這個“是”或“否”值,我需要在我的控制器中執行操作。 我們怎么做?
示例代碼:
public ActionResult ActionName(passing value)
{
// some code
message box here
if (true)
{ true code}
else { else code}
}
你可以用 ActionLink 做到這一點
@Html.ActionLink(
"Delete",
"DeleteAction",
"Product",
new { confirm = true, other_parameter = "some_more_parameter" },
new { onclick = "return confirm('Do you really want to delete this product?')" })
如果用戶確認,則鏈接參數將傳遞給控制器操作方法。
public ActionResult DeleteAction(bool confirm, string other_parameter)
{
// if user confirm to delete then this action will fire
// and you can pass true value. If not, then it is already not confirmed.
return View();
}
更新
您不能在控制器端顯示消息框。 但是你可以像下面這樣做
public ActionResult ActionName(passing value)
{
// some code
message box here
if (true){ ViewBag.Status = true }
else { ViewBag.Status = false}
return View();
}
並查看
<script type="text/javascript">
function() {
var status = '@ViewBag.Status';
if (status) {
alert("success");
} else {
alert("error");
}
}
</script>
但是這些所有代碼都不是優雅的方式。 這是您的場景的解決方案。
是的,正如 AliRıza Adıyahşi 評論的那樣,您可以使用@Html.ActionLink
來做到這一點。
訂閱@Html.ActionLink
的onclick
事件
這是實現:
@Html.ActionLink("Click here","ActionName","ControllerName",new { @onclick="return Submit();"})
並在 javascript 中編寫confirm
框。
<script type="text/javascript">
function Submit() {
if (confirm("Are you sure you want to submit ?")) {
return true;
} else {
return false;
}
}
</script>
編輯
像這樣嘗試:
<script type="text/javascript">
function Submit() {
if (confirm("Are you sure you want to submit ?")) {
document.getElementById('anchortag').href += "?isTrue=true";
} else {
document.getElementById('anchortag').href += "?isTrue=false";
}
return true;
}
</script>
@Html.ActionLink("Submit", "Somemethod", "Home", new { @onclick = "return Submit();", id = "anchortag" })
現在在您的控制器中根據isTrue
查詢字符串執行一些操作
public ActionResult Somemethod(bool isTrue)
{
if (isTrue)
{
//do something
}
else
{
//do something
}
return View();
}
您不在控制器中創建確認框,但在視圖中創建確認框,使用 JQuery 對話框。 控制器已經在服務器內部,因此您在那里沒有用戶交互。 另一方面,您的視圖是用戶選擇選項、輸入信息、單擊按鈕等的地方……您可以攔截按鈕單擊以顯示該對話框,並且僅在選項“是”時提交帖子"被點擊。
JQuery Dialog 需要頁面中引用的jquery.js 、 jquery-ui.js 、 jquery.ui.dialog.js腳本。
例子:
$(function(){
$("#buttonID").click(function(event) {
event.preventDefault();
$('<div title="Confirm Box"></div>').dialog({
open: function (event, ui) {
$(this).html("Yes or No question?");
},
close: function () {
$(this).remove();
},
resizable: false,
height: 140,
modal: true,
buttons: {
'Yes': function () {
$(this).dialog('close');
$.post('url/theValueYouWantToPass');
},
'No': function () {
$(this).dialog('close');
$.post('url/theOtherValueYouWantToPAss');
}
}
});
});
});
我可以確認 AliRıza Adıyahşi 的解決方案運行良好。
您還可以自定義消息。 就我而言,我們使用的是 MVC 和 Razor,所以我可以這樣做:
<td>
@Html.ActionLink("Delete",
"DeleteTag", new { id = t.IDTag },
new { onclick = "return confirm('Do you really want to delete the tag " + @t.Tag + "?')" })
</td>
其中顯示了一個對話框,其中包含一個特定的記錄。 也可以給確認對話框一個標題,還沒試過。
<a href="@Url.Action("DeleteBlog", new {id = @post.PostId})" class="btn btn-sm btn-danger" onclick="return confirm ('Are you sure want to delete blog?');">
<i class="glyphicon glyphicon-remove"></i> Delete
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.