1

I'm trying to initialize a jQuery call with some parameters, but I don't know how to access them.

Now I have:


// Controller code

public ActionResult Offer()
{
...
  ViewData["max"] = max;
  ViewData["min"] = min;
...
  return View(paginatedOffers);
}

// View Code

script type="text/javascript">
$().ready(
  function() {
    // Slider
       $('#slider').slider({
           min: %= Html.Encode(ViewData["min"]) %>,  
           max: %= Html.Encode(ViewData["max"]) %> 
       });

    });

/script>


But I noticed that I don't have access to ViewData inside the script tag.

Is there a mistake on my side? Can you point me in the right direction, please?

(I'm new to ASP/C#).

Thank you, M.

Edits: Start of script tag and ASP intentionally left out.

1
  • 1
    "I don't have access" - what that mean, did you tried to run your code? What error do you see? Commented Aug 18, 2009 at 6:48

2 Answers 2

2

As Mike Chaliy pointed out, it works, but you don't get intellisence. Because of a bug in my script, I thought that it doesn't work at all.

Thanks Mike (and CMS too).

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

Comments

1

You are missing some characters, the beginning of the <%%> tags, and you will need a comma to separate the min and max options:

<script type="text/javascript">
$(function() {
    // Slider
       $('#slider').slider({
           min: <%= Html.Encode(ViewData["min"]) %>,    
           max: <%= Html.Encode(ViewData["max"]) %> 
       });
    });

</script>

2 Comments

I've omitted the start < sign, because the preview interpreted it as html tag. Also, the comma is present in my code (not this sample). The problem is that I can't access ViewData inside <script>
@Marius, there is no intellisence in script tag, but it definetly should work, just run application.

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.