问题
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?
回答1:
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 ...
回答2:
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?
回答3:
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.
来源:https://stackoverflow.com/questions/6940910/mvc3-partial-view-postback-not-work