与@ HTML.Action局部视图(...)(Partial view with @HTML.Ac

2019-09-16 19:38发布

我有应显示一个用户登录到一个帐户名一个强类型的局部视图:


@model MyNamespace.Models.AccountNameViewModel

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

我有一个控制器:

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);

    }
}

我想要做的就是添加,显示,用户登录到一个帐户的名称共享的局部视图。 我得到的是以下错误:

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

我是不是至少在朝着正确的方向?

Answer 1:

你必须删除controller在您的通话部分

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

要渲染的局部视图,你也可以拨打

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


文章来源: Partial view with @HTML.Action(…)