gpt4 book ai didi

asp.net-mvc - Mvc flash 消息作为局部 View

转载 作者:行者123 更新时间:2023-12-03 21:35:53 24 4
gpt4 key购买 nike

我正在使用 FlashHelper 向用户显示 Flash 消息,我需要显示部分 View 而不是默认的 Flash 消息。我该怎么做,或者这可能还是不可能?。

我需要这样的 helper :
Flash.Success("_FlashMessagePartial",model)
或者你有什么图书馆推荐吗?

最佳答案

你不能这样使用它。如果我理解正确,您希望向用户显示类似通知的内容,并且您不希望刷新您的页面。正确答案是你可以做到,但这是不正确的。您的代码将看起来一团糟,您将有一个用于通知等的隐藏字段。这取决于你想要完成什么。例如:
我在我的布局中使用了一个全局通知,我的剪辑看起来像这样

在布局的顶部
我放置了一个 if 语句来检查 TempData 中是否存在错误消息

@if(TempData["error"])
{
// here you can visualize it well styled by some template for example
<div class="notification-error">@TempData[error]</div>
}
@if(TempData["success"])
{
// here you can visualize it well styled by some template for example
<div class="notification-error">@TempData[error]</div>
}

Then in your controller if you have something to say to the user you can
just do what you are doing like:

public ActionResult SomeAction(MyModel model)
{
if(something is not valid)
{
this.TempData["error"] user error
return this.View(model);
}
}



That case was if you need some server validation and proper message to the
user. After that option you have to set a script on your layout that removes
the error and success notifications if they exist on the page loads.

另一个选项,如果您需要在客户端进行一些模型状态验证,您可以查看由 View 模型的属性驱动的 js-unobtrusive 验证,它使您在进入服务器之前自动检查客户端。

第三个选项是,如果您需要一些其他通知,您可以使用名为 toastr 的 JS 库。 Examples library url它很轻而且很容易使用

希望这个答案准确地解释了你需要什么。

关于asp.net-mvc - Mvc flash 消息作为局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35741676/

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