【问题标题】:MVC3 partial view postback not work?MVC3 部分视图回发不起作用?
【发布时间】:2011-08-04 11:50:19
【问题描述】:

我在 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 视图呈现正确,但单击提交按钮时没有任何反应。为什么?

【问题讨论】:

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


    【解决方案1】:

    首先停止复制这一行

    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 来发布您的操作...

    【讨论】:

    • 感谢您的回答,但在使用此解决方案时,视图第一次显示验证错误,并且在提交视图时未呈现以反映用户已登录,直到刷新页面
    • 你在使用 Html.EnableClientValidation();在 ajax.beginform() 之前?并且在您完成所有检查(例如验证用户)的操作中,您不必呈现相同的 html ... 因为您已经使用了表单,所以在控件上创建另一个使用相同模型但表示用户已登录的 div或其他东西,并在此添加 UpdateTargetId attr: new AjaxOptions {UpdateTargetId = [divid], OnBegin=[someFunction], OnFailure=[failureFunction] } 所以当操作成功时它会显示这个 div 而不是那个......但它完全由您决定如何管理您呈现的 html ...
    • 谢谢,验证总是显示,因为我忘记添加样式表
    • 现在我在主视图执行 HttpPost 应用程序验证这个局部视图时遇到了另一个问题,我怎样才能停止这种行为????
    【解决方案2】:

    我看不到您的提交是否包含在表单中?

    你应该添加:

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

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

    【讨论】:

    • 卢克,首先感谢您的回答,其次它有效,但我有新问题(1)当我按下提交时,LogOn 视图显示验证错误,视图未更改以反映用户被记录直到刷新页面!!??
    • 你试过dvlpr的建议了吗?我没有注意到你在运行部分是我的错。它可能更适合您的使用?至于Request.IsAuthenticated 考虑一下...您已经提交回页面并且状态将为false,只是因为您在中途调用FormService 过程不会使Request.IsAuthenticated 更改为true。我会在 ModelViewBag 属性中设置一些内容来表示您已登录。
    • 感谢 Luke,我使用 ViewBag,现在应用程序正在运行
    【解决方案3】:

    请检查您是否在表单中添加了任何变为空的隐藏字段。简而言之,应该没有什么不需要为 null 和发布为 null。

    例如,我正在发布表单(用于创建),其中隐藏字段 ID(主要),因此 ID 在创建实例中将为空,因此模型有效并且看起来像任何未发布的东西。

    【讨论】:

    • 你介意改写你的句子吗?很难理解你想要传递的想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 2012-09-28
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多