3

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

@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.

Not permitted to use TempData , ViewBag , ViewData.

Not permitted to use Script tag in view . it has to be in a seperate js file

1
  • 2
    Why not use Javascript in the onready event to display an alert as soon as the page is rendered? Commented May 3, 2017 at 7:40

2 Answers 2

7

You could check with some simple Razor syntax

@if(!string.IsNullOrEmpty(Model.Property1)) {
    <script>alert("@Model.Property1");</script>
}
Sign up to request clarification or add additional context in comments.

4 Comments

suppose just an alert(@model.property1) . how can i call that to appear that alert
its executing (pressing f10) to alert line but alert is not appearing in my view
Are you sure you have edited "Property1" to your "property1"?
I just understood partial view wont render alert if its called again . i did a different approach to solve it
0

you can use javascript and give alert once page is loaded.

<script type="text/javascript">

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

1 Comment

my js file is different , it will not read Model.property1 . cant put js script in same view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.