[英]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.