views:

107

answers:

3

I want to show the content of ViewData, which holds string in my case, on a modal window. For this, I am using following code in my view.

<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc&lt;/a&gt;.
</p>
<form runat = "server">
<div onload = "poponload('<%=ViewData["sample"].ToString()%>')">
  <input type="submit" value="show" id="submitBtn"/>
</div>
<script type="text/javascript" language="Javascript">
  function poponload(msg) {
    alert(msg.toString().length);
    if (msg.toString().length > 0) {
      my_window = window.open("",
  "mywindow1", "status=1,width=350,height=150");
      my_window.document.write(msg.toString());
    }
  }
</script>
</form>

The controller contains simple action methods as follows.

public ActionResult Index()
    {
      ViewData["Message"] = "Welcome to ASP.NET MVC!";
      return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection collection)
    {
      ViewData["Message"] = "Welcome to ASP.NET MVC!";
      ViewData["sample"] = "My name is kaps";
      return View();
    }

I want to show the contents of ViewData["sample"] using Window.open(). But this code is unable to show me the desired output. Can anyone please tell me how can I achieve it?

Thanks.

A: 

Is the javascript running? If not, try to put the method in the jquery ready method.

Something like:

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
   $(document).ready(function() {
      alert(msg.toString().length);
      if (msg.toString().length > 0) {
         my_window = window.open("",
           "mywindow1", "status=1,width=350,height=150");
           my_window.document.write(msg.toString());
   });
</script>

If you explain a little better what happens, it would be easier to give a solution. I'm not able to run the code here, so more info is needed.

gautema
The script is not running. I wrapped the script inside ready(). But still it isn;t working.
kapil
Have you included a reference to jquery.js? This is needed to get $(document).ready working.
gautema
kapil
You can add it either in the masterpage or in the aspx. Depending on if you want to use jquery everywhere or not. Add something like:<script src="/Scripts/jquery-1.3.2.js" type="text/javascript"></script> to your file.
gautema
I have MVC2 RC2 installed. And my scripts folder contains jquery-1.4.1.js. Am I supposed to add this?
kapil
Yes, that's just a newer version. I only have MVC 1.0 installed here, and haven't bothered to upgrade jquery yet.
gautema
A: 

divs does not support the onload attribute.

move your code to the body tag instead.

<body onload = "poponload('<%=ViewData["sample"].ToString()%>')">
Robban
I don;t want to use body tag. Can;t I do this without changing the default page contents of MVC view.
kapil
A: 

I would recommend using a javascript library such as JQuery. There are many modal popup plugins that make this sort of thing very simple.

One of my favourites is Simple Modal by Eric Martin. Below is an example of how this could be used to display ViewData.

<script type="text/javascript" language="Javascript">
     $(document).ready(function() {
         $("#sample").modal();
     });        
</script>
<div id="sample"><%=ViewData["key"]%></div>

Edit:

To use window.open see below. However this is not a modal popup as it exists in a new browser window. This is likely to get blocked by popup blockers so I would not do it this way.

<script type="text/javascript" language="Javascript">
     $(document).ready(function() {
         var url = "<%= ViewData["key"].ToString() %>";
         window.open(url, "DescriptiveWindowName",
              "resizable=yes,scrollbars=yes,status=yes");
     });        
</script>

From the mozilla developer page

How can I tell when my window was blocked by a popup blocker? With the built-in popup blockers of Mozilla/Firefox and Internet Explorer 6 SP2, you have to check the return value of window.open(): it will be null if the window wasn't allowed to open. However, for most other popup blockers, there is no reliable way.

Hope this helps.

madcapnmckay
But how can I do this using Window.open()?
kapil