简体   繁体   English

如何从.cshtml视图显示警报消息 - asp.net mvc

[英]How to display an alert message from .cshtml view - asp.net mvc

I have two action result one sending a string and another loading the view 我有两个动作结果,一个发送一个字符串,另一个加载视图

public ActionResult getTheString()
{
   string message = //doing some thing ;
  myModel myModel = new myModel();
  myModel.property1 = message ;

    return loadView(myModel);
}

public ActionResult loadView(myModel model)

{
   return view (model);
}

loadView.cshtml loadView.cshtml

@model project.Models.myModel 
@{
  if(Model.property1 !=" ")
     {what to do to show it as alert }
}

Here I am getting the prperty like model.property1 if it has some thing show the alert with that string and then load, if message does not contain anything then simply load. 在这里我得到像model.property1这样的model.property1如果它有一些东西用该字符串显示警告然后加载,如果消息不包含任何东西然后只是加载。

Not permitted to use TempData , ViewBag , ViewData . 不允许使用TempDataViewBagViewData

Not permitted to use Script tag in view . 不允许在视图中使用脚本标记。 it has to be in a seperate js file 它必须在一个单独的js文件中

You could check with some simple Razor syntax 您可以使用一些简单的Razor语法进行检查

@if(!string.IsNullOrEmpty(Model.Property1)) {
    <script>alert("@Model.Property1");</script>
}

you can use javascript and give alert once page is loaded. 您可以使用javascript并在加载页面后发出警报。

<script type="text/javascript">

window.load = function() {
    @if(!string.IsNullOrEmpty(Model.Property1)) {
        alert("blah blah");
    }
}    
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在控制器 asp.net mvc 5 的视图上显示错误消息 - Display error message on the view from controller asp.net mvc 5 如何在Asp.net MVC中将值从部分视图传递到共享视图_Layout.cshtml? - How to pass value from partial view to shared view _Layout.cshtml in asp.net MVC? 如何从asp.net mvc中的视图文件(.cshtml)中的时间戳解析日期和时间? - How do I parse date and time from timestamp in view file(.cshtml) in asp.net mvc? 如何使用 ASP.NET Core MVC 识别您来自的 view.cshtml? - How to recognize the view.cshtml that you are coming from using ASP.NET Core MVC? 如何从模型属性显示 Cshtml? ASP NET 核心 MVC - How to display Cshtml from Model property? ASP NET Core MVC 如何在 ASP.NET 中显示警报消息并重定向到主页 - How to display the alert message and redirect to home page in ASP.NET ASP.Net MVC错误验证-将自定义视图模型传递给视图时如何显示验证消息 - ASP.Net MVC Error Validation - How to display validation message when passing a custom view model to a view 打开.aspx文件作为asp.net mvc中cshtml的部分视图 - Opening .aspx file as partial view of cshtml in asp.net mvc ASP.NET MVC 3 View Engine无法识别具有.cshtml的PartialView - ASP.NET MVC 3 View Engine does not recognise PartialView with .cshtml 在asp.net mvc 5中创建可编辑的cshtml视图页面 - create editable cshtml View Page in asp.net mvc 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM