0

I am trying to query data from a database, in order to take it to create the main menu in my ASP.NET MVC 2 web site.

This is the code in my controller :

public JsonResult GetMenu(string id)
{
  JsonResult jr  = new JsonResult();
  var ien_menu = (    
      from d in this.DataContext.Departments                                                    
      join c in this.DataContext.Categories                                  
      on d.ID equals c.DepartmentID   
      join i in this.DataContext.Items on 
      c.ID equals i.CategoryID
      where d.Active == 1
         && d.ActiveInWeb >0
         && c.Active > 0
         && c.ActiveInWeb >0
         && i.Active > 0
      select d
    ).Distinct()
     .OrderBy(s=>s.Name);
  jr.Data = ien_menu.ToList();
  jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
  return jr;
}

But I really have no idea how to take this data and display it in my partial views. What do I need to do?

1 Answer 1

1

How are you calling this controller action? You can use jQuery to get the Json results if you are loading via ajax. Look at the success function to get the data - http://api.jquery.com/jQuery.ajax/

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

5 Comments

Thanks for your answer. I'm not yet know that, how can I call this controller, and anyway I never know and try to get data by Jquery or Json.
in document.ready or whenever you want to load the menu json data, make a ajax call using jQuery. Assuming that your controller action's Url is example.com/foo/getmenu, then in javascript you could do something like $.ajax({url:http://example.com/foo/getmune,success:function(data){//this is where you can parse the json in data object}
Ok, thanks. But could you please tell me, how could I loop a collection of jr that I have return from my controller? by using ` $.getJSON(url, { id: t }, function (data) { $.each(data, function (index, dataOption) { }); }); ` ?
yes, you have to use $.each for iterating through items in the json.
But how can I create the link like <%: Html.ActionLink(b.Name, Model.ActionName, Model.ControllerName, new { dep = strDepLisintCon, cat = b.ID, tab = Model.Tab }, new { @class = "selectedsubcat" })%> in that script?

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.