如何在mvc控制器中创建确认框
本文关键字:创建 确认 控制器 mvc | 更新日期: 2023-09-27 18:04:58
我需要在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