开发者

Display view result in a div in ASP.NET MVC

开发者 https://www.devze.com 2023-02-22 03:47 出处:网络
I have this : <div>@Html.ActionLink(\"MyLink\", \"Index\", \"Home\")</div> <div id=\"ToDisplayResult></div>

I have this :

     <div>@Html.ActionLink("MyLink", "Index", "Home")</div>

     <div id="ToDisplayResult></div>

Is it possibl开发者_Python百科e to display the View or PartialView returned by the controller in a specific div ?

Thanks,


If you add this:

<div id="ToDisplayResult>@Html.Action("MyActionThatReturnsPartial", "Home")</div>

It will display the partial view result where you desire. Make sure that your controller returns a partial view result, of course.


<div>@Html.ActionLink("MyLink", "Index", "Home", null, new { id = "mylink" })</div>
<div id="ToDisplayResult></div>

and in a separate javascript file AJAXify:

$(function() {
    $('#mylink').click(function() {
        $('#ToDisplayResult').load(this.href);
        return false;
    });
});

And for completeness of my answer (not that this is something I would recommend) you could use the Ajax helpers:

<div>
    @Ajax.ActionLink(
        "MyLink", 
        "Index", 
        "Home", 
        new AjaxOptions { UpdateTargetId = "ToDisplayResult" }
    )
</div>
<div id="ToDisplayResult></div>

Just make sure you have included the proper unobtrusive script to your page in this case:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>


the most basic way of updating a div would be using jQuery .load

$('#ToDisplayResult').load('/SomeController/Action');

see this question for more details: How can I reload a div with a PartialView in ASP.NET MVC using jQuery?

0

精彩评论

暂无评论...
验证码 换一张
取 消