tags:

views:

179

answers:

2

How to generate a javascript link in asp.net mvc?

eg.

<a href="javascript:poptastic('/examples/poppedexample.html');">Pop it</a>

Can I use Html.ActionLink and how to do this?

I could do something like this:

<a href="javascript:poptastic('ItemDetail?ID=<%=item.ID%>');">Pop it</a> 

But I just want to find out will there be some better solutions for this?

Many thanks.

+1  A: 

Yes, you can do something like:

<%=Html.ActionLink(model.Title, "View", "PoppedView", new { Id = model.Id.ToString() }, new { target="_blank" })%> 
Ian Nelson
Thanks Ian. I need to control the size the pop up page's heigh, width and without address bar. I think you solution can not meet this requirement.
Daoming Yang
Aha, Scope creep eh? :-)
Ian Nelson
I know I could do something like this: <a href="javascript:poptastic('ItemDetail?ID=<%=item.ID%>');">Pop it</a> , but I just want to find will there be some better solutions for this?
Daoming Yang
A: 

I would look at doing this using jQuery UI and a dialog instead of a new window. You can use the open handler to load up the content into the dialog.

<%= Html.ActionLink( "Pop It",
                     "ItemDetail",
                     "Item",
                     new { ID = model.ID },
                     new { @class = "popup-link" } ) %>

<script type="text/javascript">
   $(function() {
        $('.popup-link').click( function() {
           var href = $(this).attr('href');
           $('<div><p class="popup-content"></p></div>').dialog({
              autoOpen: true,
              modal: true,
              height: 200,
              width: 400,
              open: function() {
                   $(this).find('.popup-content').load(href);
              },
              close: function() {
                   $(this).dialog('destroy');
              }
           });
           return false;
        });
   });
</script>
tvanfosson