简体   繁体   中英

Partial view with @HTML.Action(…)

I have a strongly typed partial view that should show the name of an account that a user is logged into:


@model MyNamespace.Models.AccountNameViewModel

@if (Request.IsAuthenticated)
{
    @Html.Action("AccountName", "AccountNameController", Model)
    Logged in to @Model.AccountName
}

I have a controller:

public class AccountNameController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [ChildActionOnly]
    public ActionResult AccountName(AccountNameViewModel model)
    {
        ... Do somthing with the repository to populate the model
        return PartialView(model);

    }
}

What I want to do is add a shared partial view that displays the name of an account that a user is logged into. What I get is the following error:

The controller for path '/ParentViewPath/' was not found or does not implement IController.

Am I at least heading in the right direction?

You have to remove the controller part in your call

 @Html.Action("AccountName", "AccountName", Model)

To render a partial view you can also call

 @Html.Partial("AccountName", "AccountName", Model)

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