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?

1
  • In reality I am trying to register/create JavaScript classes with EF classes but did not want to complicate the question Commented Jul 8, 2012 at 2:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.