加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 作为局部视图的Mvc flash消息

发布时间:2020-12-16 09:20:12 所属栏目:asp.Net 来源:网络整理
导读:我正在使用FlashHelper向用户显示flash消息,我需要显示部分视图而不是默认的flash消息.我怎么能这样做或者是否可能? 我需要一个这样的帮手: Flash.Success( “_ FlashMessagePartial”,型号) 或者你建议有图书馆吗? 解决方法 你不能这样使用它.如果我理解
我正在使用FlashHelper向用户显示flash消息,我需要显示部分视图而不是默认的flash消息.我怎么能这样做或者是否可能?

我需要一个这样的帮手:

Flash.Success( “_ FlashMessagePartial”,型号)

或者你建议有图书馆吗?

解决方法

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

在布局的顶部就在之前
我放置一个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.

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

第三个选项是如果你需要一些其他通知,你可以使用名为toastr的JS库. Examples library url重量轻,使用方便

希望这个答案能够准确地解释您的需求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读