Thomas
Thomas

Reputation: 34188

Regarding loading of partial view in mvc

i am new in mvc. now learning. i was searching various technique to load partial view in mvc and i got good one in stackoverflow. here it is. If you want to load the partial view directly inside the main view you could use the Html.Action helper:

@Html.Action("Load", "Home")

or if you don't want to go through the Load action use the HtmlPartial hepler:

@Html.Partial("_LoadView")

If you want to use Ajax.ActionLink, replace your Html.ActionLink with:

@Ajax.ActionLink(
    "load partial view", 
    "Load", 
    "Home", 
    new AjaxOptions { UpdateTargetId = "result" }
)

and of course you need to include a holder in your page where the partial will be displayed:

<div id="result"></div>

Also don't forget to include:

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

in your main view in order to enable Ajax.* helpers. And make sure that unobtrusive javascript is enabled in your web.config (it should be by default):

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

after going through the above code one confusion occur. help require. my confusion as below.

@Html.Action("Load", "Home")
@Html.Partial("_LoadView")

i know the use of @Html.Partial("_LoadView") but do not understand how @Html.Action("Load", "Home") will work ?

can anyone show me couple of example to show the various usage of

@Html.Action("Load", "Home")

and how it is different from @Html.Partial("_LoadView")

thanks

Upvotes: 2

Views: 1976

Answers (2)

Annish
Annish

Reputation: 2185

@Html.Action("Load", "Home")

Will execute the "Load" ActionResult in your "HomeController". This Action may return any of these (ref: MSDN):

  • ContentResult
  • EmptyResult
  • FileResult
  • HttpUnauthorizedResult
  • JavaScriptResult
  • JsonResult
  • RedirectResult
  • RedirectToRouteResult
  • ViewResultBase

While

@Html.Partial("_LoadView")

Will insert your partial view "_LoadView" into your current view.

If you're familiar with web forms, think of your partial views as .ascx (user controls).

Edit:

Example of usage of @Html.Action():

Say you have this view:

<p>Here is my name: @Html.Action("Name")</p>

And this is my controller (As you see, I use the overload of Html.Action() that implicit uses the controller you're routed to):

public class FooController : Controller
{
    //
    // GET: /Foo/

    public ActionResult Index()
    {
        return View();
    }

   // GET: /Foo/Name

    public ActionResult Name()
    {
       return Content("Annish");
    }
}

Upvotes: 1

Jatin patil
Jatin patil

Reputation: 4288

Html.Partial

  1. Renders the partial view as an HTML-encoded string.
  2. This method result can be stored in a variable, since it returns string type value.
  3. Simple to use and no need to create any action.
  4. Partial method is useful used when the displaying data in the partial view is already in the corresponding view model.For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.

    @Html.Partial("_Comments")

Html.Action

  1. Renders the partial view as an HtmlString .
  2. For this method, we need to create a child action for the rendering the partial view.
  3. This method result can be stored in a variable, since it returns string type value.
  4. Action method is useful when the displaying data in the partial view is independent from corresponding view model.For example : In a blog to show category list on each and every page, we would like to use Action method since the list of category is populated by the different model.

    @{Html.Action("Category","Home");}

Upvotes: 2

Related Questions