asp.net mvc无法渲染动作

时间:2012-03-05 11:30:00

标签: asp.net-mvc-3 partial-views renderpartial renderaction

我有一个包含以下内容的布局页面

_app.cshtml

<div id="top_right">
    @{ Html.Partial("_LogOnPartial"); }
</div>

登录部分包含

_LogOnPartial.cshtml

@{
    Layout = null;
}

@if (Request.IsAuthenticated)
{
    Html.RenderAction("Details", "Account");
}
else
{
    Html.RenderAction("LogOn", "Account");
}

和我希望看到的渲染动作方法是

AccountController.cs

public class Account
{  
        public PartialViewResult LogOn()
        {
            return PartialView();
        }
}

Account / LogOn返回的登录部分视图是

LogOn.cshtml

@model Presentation.Models.LogOnModel

<div id="login">
    @Html.ValidationSummary()

    <h2>Start by Loggin in</h2>

    @using (Html.BeginForm("LogOn", "Account"))
    {
        @Html.Hidden("returnUrl", Request.Url.PathAndQuery)

        <table width="100%" border="0" cellspacing="0" cellpadding="5">
            <tr>
                <td>
                    <span class="bluey">Username:</span><br />
                    @Html.TextBoxFor(m => m.UserName, new {tabindex = "1", Class = "field"})
                    @Html.ValidationMessageFor(m => m.UserName, "*")
                </td>
                <td>
                    <span class="bluey">Password:</span><br />
                    @Html.TextBoxFor(m => m.Password, new {tabindex = "2", Class = "field"})
                    @Html.ValidationMessageFor(m => m.Password, "*")
                </td>
            </tr>
            <tr>
                <td>
                    <input name="login" type="submit" value="Submit" class="input_btn" tabindex="3" />
                </td>
                <td>@Html.CheckBoxFor(m => m.RememberMe) @Html.LabelFor(m => m.RememberMe) <span class="bluey">&nbsp; | &nbsp;</span> @Html.ActionLink("Forgot Password?", "Password", "User")></td>
            </tr>
        </table>
    }
</div>

我已经跟踪了流程,我可以看到它正在点击

Html.RenderAction("LogOn", "Account")

并从那里返回局部视图但是当我查看页面时,没有呈现部分视图的html( LogOn.cshtml ),它甚至不存在于源中。 / p>

我错过了一些基本的东西,有没有更好的方法来实现这个目标?

1 个答案:

答案 0 :(得分:4)

你有一个代码块将返回部分而不是输出它,改变:

<div id="top_right">
    @{ Html.Partial("_LogOnPartial"); }
</div>

为:

<div id="top_right">
    @Html.Partial("_LogOnPartial");
</div>

或者,使用Html。RenderPartial直接输出。