开发者

ASP.NET MVC 3 Razor Rendering Partials from One Controllers View to Another Controllers View

开发者 https://www.devze.com 2023-02-24 08:08 出处:网络
What is the best way to redirect to another partial view from a partial view without refreshing the main page. So 1 partial view changing into another partial view after a button click. Can you do thi

What is the best way to redirect to another partial view from a partial view without refreshing the main page. So 1 partial view changing into another partial view after a button click. Can you do this with 开发者_StackOverflow中文版jQuery , or is there a better way to perform this? Also you need to pass an id with it


AJAX seems like a good solution in this case. So you could place a button somewhere on the page:

@Html.ActionLink(
    "link text",
    "someAction",
    "someController",
    new { id = "put here some id you want to send to server" },
    new { id = "myLink" }
)

<div id="partial2Div"></div>

and then unobtrusively AJAXify this link in a separate javascript file:

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

The controller action will simply return the corresponding partial view:

public ActionResult SomeAction(string id)
{
    var model = ...
    return PartialView(model);
}


Ajax.ActionLink will do this for you out of the box without having to write additional javascript code.

@Ajax.ActionLink("Link Text","Action","Controller",new {id= myId},new AjaxOptions{InsertionMode=InsertionMode.Replace, HttpMethod="get", UpdateTargetId="target-location-id"})
0

精彩评论

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

关注公众号