Marius
Marius

Reputation: 3617

How to access some data inside script tag in ASP.NET MVC?

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.

Upvotes: 1

Views: 1000

Answers (2)

Marius
Marius

Reputation: 3617

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

Upvotes: 2

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827466

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>

Upvotes: 1

Related Questions