c# - How do I pass a variable from a view to a JavaScript function? -
i'm working on website has javascript function want call passing in value when hyperlink clicked. i'm generating table rows in view this:
foreach(var e in model.collection) { ... //this example piece of code tries big picture //across. want call js function below passing in shipaddress property <td><a href="javascript:popup(@e.shipaddress)">@e.shippeddate</a></td> ... } and have javascript function this:
function popup(data) { $('#lblshipaddress').text(data.address1); ... // rest of code fill out labels in div $('#divshipinfo').dialog('open'); } i'm having issues getting shipaddress property(which has number of properties i.e. address1, address2, etc.) passed javascript function view. href="javascript:popup(@e.shipaddress)" part doesn't work, , i've tried using data attributes e.g.
<a href="javascript:popup();" data-shipaddress="@e.shipaddress" /> but have not had luck.
edit: added clarity i'm looking for. if @ possible pass shipaddress property function.
by default, @e.shipaddress going return equivalent of .tostring(), not useful method input. instead, you'll want json serialize object like:
<a href="javascript:popup(@json.encode(e.shipaddress))">@e.shippeddate</a> this result in final html like:
<a href="javascript:popup({ address1: 'blah', address2: ... })">@e.shippeddate</a> and popup method have access object values.
warning: make sure there aren't fields on address object don't want leaked end consumers - json.encode serialize public properties.
Comments
Post a Comment