1

This is what I do in the controller(It works fine) :

public JsonResult DepartmentList(string id)
{
JsonResult jr = new JsonResult();

var _menu = from a in DataContext.GetDepartment()
            select new { ID = a.ID, Name = a.Name };

jr.Data = _menu.ToList();
jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return jr;
}

And I want to use javascript to take the JSON that I have return in the below controller to display in a list and get the result :

<a href="blahblah.com/Products/dep=1?tab=2"> Department name1 </a>
<a href="blahblah.com/Products/dep=2?tab=2"> Department name2 </a>
....

Thanks.

3
  • Can you post a JSON you get back? (Raw JSON) Commented Mar 13, 2012 at 8:57
  • I would be nice to know how your JSON looks like. Don't you use .getJSON() or other method's in jQuery to get the JSON ? Commented Mar 13, 2012 at 8:59
  • @devdRew : Thanks devdRew , but I really don't know how to know that RAW JSON result, could u tell me how to bug that? Commented Mar 13, 2012 at 9:05

3 Answers 3

1

http://api.jquery.com/jQuery.getJSON/

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

Comments

1

You could use AJAX:

$.ajax({
    url: '@Url.Action("DepartmentList", new { id = "123" })',
    type: 'GET',
    success: function(result) {
        // the result variable here will represent the list returned by your controller
        $.each(result, function() {
            // we are looping through the elements of the list 
            // and each element will have an ID and Name property that
            // you could use here:
            alert('id=' + this.ID + ', name=' + this.Name);
        });
    } 
});

For example if you wanted to generate a list of anchors and append them to the end of the body:

success: function(result) {
    $.each(result, function() {
        $('body').append(
            $('<a/>', {
                text: this.Name,
                href: 'blahblah.com/Products/dep=' + this.ID + '&tab=2'
            })
        );
    });
} 

4 Comments

Thanks Darin Dimitrov, But I really don't know how to loop the elements. I'm just a pretty new to JSON.
@socheata, you could use the $.each method as shown in my answer.
I'm not mean to use the Static parameter like /Products/ , But I want to put the properties of the model, such as Model.ControllerName/Model.ActionName/...
@socheata, can't you concatenate them: href: 'blahblah.com/' + this.Property1 + '/dep=' + this.ID + '&tab=2'? And if you don't know the property names in advance you could use a for loop: for (var propName in this) { ... you could use this[propName] here to get get the value ... }
1

Using ajax,

$.post("DepartmentList", {id: "yourID"}, ajaxOK);
    function ajaxOK(data){     
      for(i=0; i< data.length; i++){
      $('a:nth-child('+(i+1)+')').text(data[i]);
      }
    }

Comments

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.