0

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

1
  • Thanks for your comments ! In fact I managed to do in a different way.. I parsed the model with @html.hidden and then I access it from javascript document.getElementById. However this is not a clean approach... I need to see how to get the list into a javascript array. May 30, 2012 at 17:33

2 Answers 2

1

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.

1
  • Thanks for your comments ! In fact I managed to do in a different way.. I parsed the model with @html.hidden and then I access it from javascript document.getElementById. However this is not a clean approach... I need to see how to get the list into a javascript array. May 31, 2012 at 10:35
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.