我有一个名为LogOn的局部视图,我基本上将登录输入复制到控件中。我使用Html.RenderPartial将控件放在我的Index.Html里面的Ajax.BeginForm
<div id="login_ajaxtarget">
<% using (Ajax.BeginForm("Logon", "Account", new AjaxOptions { UpdateTargetId = "login_ajaxtarget", HttpMethod = "Post" })) { %>
<% Html.RenderPartial("LogOn"); %>
<% } %>
</div>
我正在尝试传回验证消息并让它们显示但我似乎无法让它工作。我将模型传递给视图,但它似乎没有正确地呈现验证。
我的控制器
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
{
if (!ValidateLogOn(userName, password))
{
return PartialView("LogOn", ModelState);
//return RedirectToAction("Index", "Home");
}
FormsAuth.SignIn(userName, rememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
我的部分观点
<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="password">Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
我正在尝试使用此主题(http://forums.asp.net/p/1398814/3023892.aspx#3023892)中的建议,但我不确定这是否正确。我真正想要的是能够将LogOn功能放在主页上,而不必导航到新页面以便使用它。如果有一个更容易的方法来做到这一点,我所有的耳朵!提前谢谢。
答案 0 :(得分:0)
我认为有些事情是必需的。您没有检查输入是否为空,并且未向模型状态添加任何错误消息。
// In your action add somethings like bellow.
if (String.IsNullOrEmpty(userName))
this.ModelState.AddModelError("UserName", "The username is required.");
if (this.ModelState.IsValid)
{
// Do login
}
return PartialView("Login");
答案 1 :(得分:0)
检查出来可能会解决您的问题。
控件:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions() { UpdateTargetId = "UpdatePanel" }))
{ %>
<div id="UpdatePanel">
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">
Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="password">
Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<%= Html.CheckBox("rememberMe") %>
<label class="inline" for="rememberMe">
Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>
代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
{
if (!ValidateLogOn(userName, password))
{
return PartialView("Login");
}
FormsAuth.SignIn(userName, rememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
它对我有用。如果问题仍然存在,请使用Firebug检查回复和请求。
希望这有帮助