Hugo Hilário
Hugo Hilário

Reputation: 2908

MVC Razor and Javascript

I'm facing a challenge here with MVC 3 with razor.

So the situation is quite simple.

Controller

 public ActionResult TreeView()
    { return PartialView(Context.LEFT_NAVIGATION_T.ToList()); }

The Context returns a list of entities...

Now what I want to do is:

On the view I have the following javascript :

<script type="text/javascript">

function OnTreeViewNodeClick(s, e)
{
var tn= e.node.name;
//test
alert('@Model.Find(x => x.ID == ???).TITLE'); 

}

Ok so the onTreeViewNodeClick is an event assigned to a Treeview.

What I want to do is substitute the ??? on the razor expression with the var tn.

Is this possible ?

Thanks

Upvotes: 0

Views: 487

Answers (2)

Joel Etherton
Joel Etherton

Reputation: 37543

You can't mix that kind of parsing in this manner. You'll either need to parse the model out into a collection of viable javascript objects to search through or use AJAX to perform this kind of lookup. If you're dealing with small subsets of data, my preference would be to create a custom class and include your model collection as a collection of serialized objects within that class. If the data is large, you'll have much better luck using AJAX to retrieve individual json objects.

Upvotes: 1

Nikola Sivkov
Nikola Sivkov

Reputation: 2852

You will either have to have the Tree in Json/array or do an ajax call to get the data.

Upvotes: 0

Related Questions