1

I'm trying to do the following thing:

<div>javascript:processData(Model.Property)</div>
, where "processData" is a function that returns a string. How can this be done?

(This obviously is not working, it just returns the string "javascript:processData(Model.Property)".)

Sandra S.
  • 181
  • 1
  • 3
  • 22

3 Answers3

0
<script type="text/javascript">
function processData(property) {
    // all kinds of stuff...
    // return result;
}
</script>
...
<input type="button" onClick="return processData(<%: Model.Property %>); />

If you're using Razor syntax, replace <%: Model.Property %> with @Model.Property.

As for displaying the result, use the javascript to put the value somewhere on the page (such as the text of an <a> element or an alert).

Yehuda Shapira
  • 8,460
  • 5
  • 44
  • 66
0

Read the following question : Using Razor within JavaScript

You need razor text markers: http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

Community
  • 1
  • 1
Yasser Sinjab
  • 578
  • 6
  • 19
0

You can call the javascript method on jquery document ready and set the result to that div using html method.

@model YourSomeModelWithThatProperty
<div id="thatDiv"></div>

<script type="text/javascript">
  $(function(){
       var thatDataString=processData("@Model.Property");
       $("#thatDiv").html(thatDataString);
  });
</script>
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • yes, I know about this method...the problem is much more complex and I have tried to simplify it in my question, finally, if I can't find a solution I'll go with this method. But still, I think there is a simple way to do this. or isn't it? Can't I just call somehow javascript function inside the div? Anyway, your answer is the closest to what I was searching for – Sandra S. Oct 11 '12 at 19:39