简体   繁体   中英

Load partial view in a div MVC

I have a button on my MVC view on click of it, it should add a partial view in a 'div', by calling an action which takes an object as a parameter

I tried out some thing like this:

$('#buttonId').onclick(function(){

$('#divid').load(@Html.Action("ActionName","ControllerName",new{parameterName = objectToPass}))

});

but it loads the actionresult/partial view on page load itself not a click of button

Any Idea?

Try to use

@Url.Action

instead of

@Html.Action

Or you can use ajax, for example:

$('#buttonId').click( function() {
    $.ajax({
        type: 'POST',
        url: '@Url.Content("~/ControllerName/ActionName")',
        data: objectToPass,
        success: function (data) {
           $('#divid').innerHTML = data;
        }
    });
}

Load Partial View in a div MVC 4

Recently I want load Partal View in Div , after doing lots of R&D and It's work for me

$.ajax({
    type: 'POST',
    url: '@Url.Content("~/ControllerName/ActionName")',
    data: {
        title: title
    },
    success: function(result) {
        $('#divid').innerHTML = result;
    }
});

And In Partal View Action Controller Code

public PartialViewResult ShowCategoryForm(string title)
{
    Model model = new Model();
    model.Title = title;
    return PartialView("~/Views/ControllerName/PartalView.cshtml", model);
}
<script type="text/javascript">
    $(document).ready(function () {
        $('#buttonId').click(function () {
            $('#divid').load('@Url.Action("About", "Home")');
        });
    });
</script>

<a id="buttonId">Load partial view or Any URL</a>

<div id="divid" style="height:100px; width:400px";>

</div>

load require a string, you are generating a variable path if you look at your source code it generate something like:

$('#buttonId').click(function(){
    $('#divid').load(yoururl/);
});

but what you want is:

$('#buttonId').click(function(){
    $('#divid').load("yoururl/");
});

the code should look like:

$('#buttonId').click(function(){
    $('#divid').load('@Html.Action("ActionName","ControllerName",new{parameterName = objectToPass})');
});

but as I can see the load should be working until you click on #buttonId

On ajax call success function append result

ajax call=>

url: 'controllername/methodname'
 success: function (partialviewresult)
{ 
          $('#div').append(partialviewresult);
}

  // inside controller
 public ActionResult methodname()
{
    return PartialView("~/a/a.cshtml");
}

and controller must return partialview as result

Instead of innerHTML, html(data) worked for me;

$('#buttonId').click( function() {
$.ajax({
    type: 'POST',
    url: '@Url.Content("~/ControllerName/ActionName")',
    data: objectToPass,
    success: function (data) {
       $('#divid').html(data);
    }
});}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM