1

I have partial view for rating. This view is repeated multiple time on a single page. I have written the functional code for rating in an external js file. Now I want to call that js file each time rating view is loaded. Also I want to pass parameter to the external js file. I have tried the following method to do this:

<script type="text/javascript" >
  var EntityId="@ViewData["entityid"]";// parameters which i want to pass
  var RatingValue="@ViewData["rating"]";// parameters which i want to pass
  var EntityName="@ViewData["entityname"]";// parameters which i want to pass
</script>

<script type="text/javascript" src="../../Scripts/RatingControl.js"></script>

The code above is working but the external script only access last value of passed variable e.g. there is 5 records on my page then the rating control will be added 5 time with different value. But in external file I only get last records rating value.

So how can I call external js file from javascript code on each partial view load event.

1 Answer 1

2

Try using jquery .getScript() , that way you can control the order your scripts are executed, like this:

<script type="text/javascript" >
 var EntityId="@ViewData["entityid"]";// parameters which i want to pass
 var RatingValue="@ViewData["rating"]";// parameters which i want to pass
 var EntityName="@ViewData["entityname"]";// parameters which i want to pass
 $.getScript("../../Scripts/RatingControl.js");

 //second time
 var EntityId="new value";// parameters which i want to pass
 var RatingValue="new value";// parameters which i want to pass
 var EntityName="new value";// parameters which i want to pass
 $.getScript("../../Scripts/RatingControl.js");
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks nelson it works, but in external file i get only last records entity id

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.