views:

131

answers:

2

i have an asp.net mvc view where the top of the page is a table. What i want is to click on a row in the table which will go to the server and get back details on that table. This is working fine and i am returning the details in Json.

The issue is i know want to show a details panel below. Right now i have the details pane all complete in a partial view.

How would i "hook this up" using jquery so when i click on the row, the details pane shows up with the correct model data filled out.

  1. Do i ditch the json and simply generate html in my controller action and return html to the view to display the details pane ?
  2. Other best practices or suggestions here ?
A: 

I would go for a client side template engine like jtemplates (jquery plugin) to handle this client side. You can simply return a partialview from the controller action and render that html where you want it on the page. But I consider it bad practice to return html from a ajax request and I would much rather return a json object.

Mattias Jakobsson
that is exactly my dillema. it seems easier to return the html to the view but it seems to break seperation of concerns . .
ooo
Then try jtemplates (the link I mentioned). With that you can "bind" a json object to a predefined template much like you do with a model to a view but client side instead. There are also other engines like that if you don't like this or don't use jquery.
Mattias Jakobsson
+2  A: 

Hi,

I'm assuming your using the MVC Ajax helpers to call your controller to get your JSon result?

An alternative to that would be using JQuery to call your JSon controller. So on a link or button click you could call some javascript which finds the id you need to pass to the controller from a hidden tag. When you receive your JSon results back you just set the properties of the html tag and show/hide your detail pane. Something like this:

function ShowPartial() {
  var ID = $("#hiddenValue").attr("value");
    $.getJSON("YourUrl" + ID, function(result) {
        $.each(result, function(item) {
          // Set you html properties using this["JSonPropertyName"]
        });
    });
}

An alternative is to simply use the MVC Ajax helpers and update a div tag with your partial details view and not use JSon.

Simon G
i am using jquery $.getJson to call my controlller actions and return json
ooo
With this approach you will have to generate your html yourself using javascript. And you need to mix in html that shouldn't be there when you load the page. That is, in my opinion, bad practice and I would probably consider returning html from the controller action a better approach.
Mattias Jakobsson
you could already have all the html generated and just set the values and un-hide them when the json is returned, but I agree this is not a best solution and returning the html from the controller is the best approach.
Simon G
Then what do you do when you need to loop out a menu? You always need to render some html in that case. I don't think you should send back html from the controller action, I think you should use a client side template engine like I suggested in my answer.
Mattias Jakobsson