Rakib Jahan Khan
Rakib Jahan Khan

Reputation: 1

Partial rendering in MVC3

I'm trying to render a particular section/div click a particular link or button. Suppose link/button is in the A.cshtml page , and b.cshtml is a partial view that I want to load in A.cshtml page within a particular section/div. I tried Ajax.ActionLink but can't do. Any help or suggestions?

Upvotes: 0

Views: 1043

Answers (3)

Amir Ismail
Amir Ismail

Reputation: 3883

Ajax.ActionLink should do it, may be you missed somwthing. Check this post it may give you the answer

Upvotes: 0

Dennis Traub
Dennis Traub

Reputation: 51634

The controller can return a partial view as action result:

public ActionResult Details()
{
    var model = // your model
    var viewName = // your partial view name
    return PartialView(viewName, model);
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

I tried ajaxactionlink but cant do

That's really not the way to ask a question here. Cant do is not a precise problem description. Next time when you ask a question on SO show what you have tried.

This being said, let me provide you with an example:

@Ajax.ActionLink("click me", "SomeAction", new AjaxOptions {
    UpdateTargetId = "result"
})
<div id="result"></div>

and then you will have an action which will render this partial view:

public ActionResult SomeAction()
{
    return PartialView("_NameOfYourPartial");
}

Finally make sure that you have referenced the jquery.unobtrusive-ajax.js script to your page which uses the HTML5 data-* attributes emitted by the Ajax.ActionLink helper to hijack the click event and send an AJAX request instead of the normal request:

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

Upvotes: 1

Related Questions