gpt4 book ai didi

c# - 如何在 mvc Controller 中创建确认框?

转载 作者:可可西里 更新时间:2023-11-01 08:23:25 25 4
gpt4 key购买 nike

我需要在 mvc Controller 中创建确认框吗?使用这个"is"或“否”值,我需要在我的 Controller 中执行操作。我们如何做到这一点?

示例代码:

    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?')" })

如果用户确认,则链接参数将传递给 Controller ​​操作方法。

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();
}

更新

您不能在 Controller 端显示消息框。但是你可以像下面那样做

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>

但是这些代码都不是优雅的方式。这是您的场景的解决方案。

关于c# - 如何在 mvc Controller 中创建确认框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15548073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com