我有一個名爲LogOn的局部視圖,其中我基本上將登錄輸入複製到控件中。我使用Html.RenderPartial放置在我的index.html控制的Ajax.BeginFormASP.NET MVC +模型狀態和局部視圖
<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功能放置在主頁上,而不必導航到新頁面才能使用它。如果有一個更簡單的方法來做到這一點,我所有的耳朵!提前致謝。
對不起,我添加的代碼是我迄今爲止改變的代碼。我已經實現了ValidateLogOn方法(或者更具體地說,它已經爲我實現了)。我只是試圖改變開始一個新的MVC項目時提供的開箱即用的網站模板。我的問題是,一旦錯誤被添加到模型中,它們就不會出現在我的頁面的驗證消息中。 – jimbo 2009-10-13 12:57:33