MVC3局部视图回发不起作用?

时间:2011-08-04 11:50:20

标签: asp.net-mvc-3 razor asp.net-mvc-partialview

我在AccountController中有LogOn局部视图:

    public ActionResult LogOn()
    {
        return PartialView();
    }

    //
    // POST: /Account/LogOn

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                }
                else
                {
                    ModelState.AddModelError("", Resources.Account.Account.LoginFailureText);
                }
            }
        }

        return PartialView(model);
    }

,我在_Layout.cshtml中将此部分渲染为:

@{Html.RenderAction("LogOn", "Account");}

和LogOn.cshtml视图是:

@model Kalimat.Web.Models.LogOnModel

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

<div class="leftbanner login">
    <div class="bookicon">
        <img src="@Url.Content(@Kalimat.Web.Resources.Common.bookiconImgSrc)" alt="@Kalimat.Web.Resources.Common.bookiconImgAlt" />
    </div>
    <div class="strip">
        @MvcHtmlString.Create(Kalimat.Web.Resources.Account.Account.LoginTitle)
    </div>
    <div class="shade_2">
        <img src="@Url.Content(@Kalimat.Web.Resources.Common.shade_2ImgSrc)" alt="@Kalimat.Web.Resources.Common.shade_2ImgAlt" />
    </div>
    <div class="insidetext">
        @{
            //Logined user view
            if (Request.IsAuthenticated)
            {
                <div id="LoginView1">
                    @{<text>@Kalimat.Web.Resources.Account.Account.WelcomeText <strong>@Model.UserName</strong>!</text>}
                    <br />
                    <a id="LoginStatus1" href="">@Kalimat.Web.Resources.Account.Account.LoginStatusText</a>
                    <a id="ChangePassLinkButton" href="">@Kalimat.Web.Resources.Account.Account.ChangePswText</a>
                </div>
            }
            else
            {
                @Html.LabelFor(m => m.UserName)
                @Html.TextBoxFor(m => m.UserName, new { @class = "inputfield" })
                @Html.ValidationMessageFor(m => m.UserName, "*")
                <br />
                @Html.LabelFor(m => m.Password)
                @Html.PasswordFor(m => m.Password, new { @class = "inputfield" })
                @Html.ValidationMessageFor(m => m.Password, "*")
                <br />
                <span class="remove_shop">
                    @Html.CheckBoxFor(m => m.RememberMe)
                    @Html.LabelFor(m => m.RememberMe)
                </span>
                <p>
                    ***<input type="submit" class="submit_Cart" value="@Kalimat.Web.Resources.Account.Account.LoginButtonText" />***
                </p>
                @Html.ValidationSummary(false, Kalimat.Web.Resources.Account.Account.LoginFailureText)
                <a class="submit_Cart" href="">@Kalimat.Web.Resources.Account.Account.NewAccountText</a>
                <br />
                <a class="submit_Cart" href="">@Kalimat.Web.Resources.Account.Account.RecoverPswText</a>
            }
        }
    </div>
</div>

当我运行应用程序时,LogOn视图呈现正确但在单击提交按钮时没有任何反应。为什么呢?

3 个答案:

答案 0 :(得分:4)

首先停止复制此行

if (ModelState.IsValid) {}

没有意义......

通过查看你的代码,你有提交按钮和一切,但我认为你错过了

@using(Ajax.BeginForm())
{
     //put all your html element in this
     // and also put submit button in it ...
}

这是Ajax.BeginForm()的参数:

Ajax.BeginForm(
    string "ActionName",
    string "ControllerName",
    new routevalues {id="IDValue",message="MyMessage"},
    new AjaxOptions {OnBegin=[someFunction], OnFailure=[failureFunction] },
    new { id = "FormName" }
)

这是因为您使用ajax发布您的行动......

答案 1 :(得分:0)

我看不出你是否在表格中提交了你的作品?

你应该补充:

@using(Html.BeginForm("Logon", "Account"))
{
    // Submit form bits here
}

围绕您的登录位提交信息?

答案 2 :(得分:-1)

请检查您是否在表单中添加了任何隐藏字段。简单来说,应该没有任何不需要null和发布为null。

例如,我发布了带有ID(主要)隐藏字段的表单(For create),因此Id在创建实例中将为null,因此模型有效且看起来像任何未发布的内容。