[英]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);
}
@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
. 不允许使用TempData
, ViewBag
, ViewData
。
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.