0

I have an view with simple script as such:

 <script type="txt/javascript">
   function DoSomething(param)
   {
     alert(param);
   }
 </script>

Is there anyway I can call that function with a value in a controller action method return result?

Edit:

I really am looking to create/register JavaScript classes with Entity Framework values. If it cannot be done how can I create/register Javascript classes with EntityFramework values?

OK, here's the thing: I already have a JS function something like this:

RegisterClasses(param1, param2)
   {
     ......
   }

I have seen it done elsewhere thru Fiddler but do not know how they do it.

How can I pass values to that JS function to create classes from values in EF?

Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58
Joe Grasso
  • 465
  • 2
  • 5
  • 19
  • In reality I am trying to register/create JavaScript classes with EF classes but did not want to complicate the question – Joe Grasso Jul 08 '12 at 02:08

2 Answers2

1

What you can do is to add this to the view:

<script type="text/javascript">DoSomething(@Model.Param)</script>

adding a Param property to that action view model.

For using EF classes, you can do this:

<script type="text/javascript">
    var jsModel = { 
                     name: "@Model.Name",
                     age: @Model.Age
                  };
</script>

Also, yo can just convert any object to json following this answer: Convert .Net object to JSON object in the view

Hope it helps.

Community
  • 1
  • 1
Ivo
  • 8,172
  • 5
  • 27
  • 42
1

You could JSON serialize your model to the view:

@model MyViewModel

<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));

    // now the model variable is a JSON representation of your
    // server side model that was passed by the controller action
    // to this view and you could manipulate it as a standard js variable


    // So assuming your model contains 2 properties Param1 and Param2 you
    // could pass them to your javascript method
    RegisterClasses(model.Param1, model.Param2);

</script>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928