views:

272

answers:

2

Hi,

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  A: 

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>
CMS
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 Ursache
@Marius, there is no intellisence in script tag, but it definetly should work, just run application.
Mike Chaliy
+1  A: 

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).

Marius Ursache