-1

I need to render a partial view in a view using ajax, but not in page load. I want render this partial when a drop value was changed. This partial should have a model and load a jsTree from model information. When I searched for a solution I found following code.

$.ajax({
        url: '/Home/Details',
        contentType: 'application/html; charset=utf-8',
        type: 'GET',
        dataType: 'html'

    }).success(function (result) {
$('#sectionContents').html(result);}).error(function (xhr, status) {
alert(xhr.responseText);});

But I need to render a partial with its own model. This code gets html result which generates after a view will be rendered. Can anybody help please?

Azad
  • 407
  • 1
  • 9
  • 22

1 Answers1

0

You should only pass limited data back to the server for your partial view.

Basically create a controller action that returns a partial view, with the model based on a unique id value you provide via the URL.

public ActionResult SomeView(int id)
{
    var vm = db.SomeTable.Find(id);
    if (vm == null)
    {
        return HttpNotFound();
    }
    return PartialView(vm);
}

And call it from Ajax with /SomeController/SomeView/idvalue etc

Using PartialView avoids including a Layout Master Page, so you just get that HTML for one panel etc.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202