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

发布于 2024-11-27 21:49:10 字数 3738 浏览 2 评论 0原文

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

I have LogOn partial view in the AccountController :

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

, I render this partial in my _Layout.cshtml with:

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

and the LogOn.cshtml view is :

@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>

when I run the application the LogOn view render correct but when clicking on the submit button nothing happens. why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝戈者 2024-12-04 21:49:10

首先停止复制这一行

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 来发布你的行动...

first stop duplicating this line

if (ModelState.IsValid) {}

doesn't make sense ...

as by looking to your code, you have submit button and everything but i think you missing

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

here is the parameters for Ajax.BeginForm():

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

this is because you using ajax to post your action ...

漆黑的白昼 2024-12-04 21:49:10

我看不到您是否以表格形式提交?

您应该在您的登录位周围添加:

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

来提交信息?

I cannot see if you have your submit in a form?

you should be adding:

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

around your login bits to submit the information?

人间☆小暴躁 2024-12-04 21:49:10

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

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

Please, check if you have added any hidden field in the form that is going null. In simple words, there should be nothing that not required to null and posting as null.

For example, I was posting form (For create) with hidden field of ID (Primary), so Id will be null in the create instance so model is in valid and its looking like any thing not posting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文