tags:

views:

98

answers:

1

hello i have js file for the popup window. i have used in asp.net but its not working in asp.net mvc can anyone tell me how to use this code in mvc

<script type="text/JavaScript">
function openUserSavedListDetailsPopUp(id) {
    dhtmlmodal.open('UserInfo', 'iframe',
           'UserSavedListDetails.aspx?listId=' + id,
           '', 'width=710px,height=150px,center=1,resize=0,scrolling=0')
}
</script>

on page:

 <a  href="#" onclick='openUserSavedListDetailsPopUp(<%# DataBinder.Eval(Container.DataItem, "OrderId")%>)' > hello </a>

thank you in advance

A: 

The statement that is (probably) not working in your MVC app is the call to <%# DataBinder.Eval(...) %>. In MVC, I assume you have a for loop or something that iterates over your model? Maybe something like this?*

<% foreach(var item in Model)
{ %>
    <a href="#" onclick='openUserSavedListDetailsPopUp(<%: item.OrderId %>)>hello</a>
<% } %>

I'd also recommend using some type of javascript library, for example jQuery, to hook up the onclick events instead of having the javascript inline. For this particular case jQuery might not add much value to your application (except that your code will probably be cleaner), but it really helps for anything from ajax calls to DOM manipulation and animation.

And finally, you should have a fallback for browsers that don't support (or have disabled) javascript. The easy way is to change your link to this:

<a href="urltothepopupcontentpage.html?id=<%: item.OrderId %>" onclick='openUserSavedListDetailsPopup(<%: item.OrderId %>); return false'>hello</a>

The call to return false will ensure that if your script can run, it stops the default action (following the link).

*) Note: using <%: ... %> instead of <%= Html.Encode(...) %> requires that you use ASP.NET 4. If you are running .NET 3.5, you use the = sign as usual.

Tomas Lycken