1

I have some records. Upon clicking on every record there information needs to display in an accordion.

That information is supposed to fetch from database dynamically.

What i've done so far is

Create a partial view. That is suppose to display the detailed information.

Upon click on record, i call jquery method and execute my method on controller. Controller returns object in the form of Json(or any other thing, open for any suggestions).

Now JQuery method has that (Model)object, but how could i use it to render my partial view from it.

tereško
  • 58,060
  • 25
  • 98
  • 150
manav inder
  • 3,531
  • 16
  • 45
  • 62

2 Answers2

2

I have some records. Upon clicking on every record there information needs to display in an accordion.

There are two ways you can achieve what you desire. I guess you have to return a partial view from the action that gives a detailed information about the record.

  1. Listening to the click event of the anchor links and inside the event you have to frame the url and then $("#accordion-container-1").load(url).

Ex.

From your comment I see you have to pass the orderNo the action as parameter. So you have to set the orderNo as id or append that to some string (to avoid duplicate ids in elements) and set to the id for the anchor link.

Then,

$(function(){

  $("a.somecssclass").click(function(){

     var orderNo = this.id;

     var url = "/ControllerName/Tracking?orderNo=" + orderNo;

     // this will call the action through ajax and update the container with the
     // partial view's html.
     $("#divToLoadTheHtml").load(url); 
  });   
});
  1. Using ajax action links supported by MVC. You can create an action link using Ajax.ActionLink that call some controller action through ajax and update the html result to a container.

Ex.

In this case when you are generating the records through looping the collection you have to create the links (on click has to load the content through ajax) through Ajax.ActionLink method and also you have to include the jquery'unobtrusive.ajax.js library.

@foreach(var m in Collection)
{
    .. other stuff

    @Ajax.ActionLink(link name, "Action", new { orderNo = m.something? }, 
    new AjaxOptions
    { 
        UpdateTargetId = "somediv" // set the div name where you want to load the partial view
    });
}

Update based on OP's comment

Your action method should be something like this,

public PartialViewResult Tracking(int orderNo) 
{ 
     var manager = new OrderManager(); 
     return PartialView(manager.Tracking(orderNo));
}

You should have a partial view either with the name Tracking.cshtml and inside the partial view you have to create the html the represents the detailed information of the record that you were talking about.

Tracking.cshtml

@model TrackingModel

<div>
  @Html.DisplayFor(...)
  ...
</div>

When you call the action Tracking from jquery or through ajax action (as i described previously) you will get partial view html that you can load into a particular container like div.

VJAI
  • 32,167
  • 23
  • 102
  • 164
  • Let me tell you where i am right now. Upon click using Jquery Ajax i request my controller and getting the data in Json format (i preferred partial view more). But the data is in raw format. I need to apply all the html on my own. And also i need to set inner html for my div. – manav inder Jul 19 '12 at 11:40
  • similar to what Zach suggested in his approach – manav inder Jul 19 '12 at 11:41
  • Can't you change the action return partial view? if not then you have to try jquery templates to convert the JSON into html and update the divs. – VJAI Jul 19 '12 at 11:50
  • Yes i can easily changed the return type of my Action to partial view. So Is this method will return me the model for the partial and then i just have to render the partial view using that returned model? – manav inder Jul 19 '12 at 12:02
  • Can you post your action code? I'm not quite understand what you are trying – VJAI Jul 19 '12 at 12:09
  • I won't think it'll help you much but this is the action 'public PartialViewResult Tracking(int orderNo) { var manager = new OrderManager(); return manager.Tracking(orderNo); //tracking is of type OrderManager and this is the model for my PartialViewResult }` – manav inder Jul 19 '12 at 12:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14134/discussion-between-msingh-and-mark) – manav inder Jul 19 '12 at 12:39
  • Thanks Mark for your help, I am already there. Kindly update your answer once more as i am having problem with using the returned object. About how to use that object in both ways jquery and Ajax.Action. It'll be a great help. – manav inder Jul 19 '12 at 12:46
  • Big Thanks. Thanks for your patience and co-operation. Please keep up the good work of helping people. – manav inder Jul 20 '12 at 14:54
0

this is a basic approach when you need submit some form and render the partial view as result

function GetView(){
if ($('#MyHtmlForm').valid()){
    $.ajax(
    {
      type: "POST",
      url: $('#MyHtmlForm').attr("action"), 
      data: $('#MyHtmlForm').serialize(),
      success: function(result) {
        //Render the partial view
        }
      },
      error: function(req, status, err) {
        //handle the error
      }
    });
}

}

and this for get basic details of row via json format, so use javascritp to generate the html

function GetSomeData() {
var cod = $('.row').val();
$.getJSON('@Url.action("ActionName","Controller")', { cod: cod }, function (result) {
    //Render data
});

}

Zach dev
  • 1,610
  • 8
  • 15
  • 1
    Dont you think writing the whole html from an object to generate the html is too much. There must be some way to pass this object to partial object as model and it'll do everything on its own. – manav inder Jul 14 '12 at 21:21
  • that's right, for that case is the first example, you get the html ready just to `$('#someConteirner').html(result);` – Zach dev Jul 14 '12 at 23:26