好吧,我有一個視圖模型,看起來像這樣:驗證不工作的視圖模型
public class UserLogin
{
[Required]
public string EmailAddress { get; set; }
[Required]
public string Password { get; set; }
}
我的控制器看起來是這樣的:
[HttpPost]
public ActionResult LogIn(UserLogin model)
{
if (!ModelState.IsValid)
{
// ...
}
}
我的看法是這樣的:
<% Html.BeginForm("Join", "User", FormMethod.Post); %>
<%= Html.Hidden("ReturnUrl", Request.Url.AbsolutePath) %>
<%= Html.TextBoxFor(c => c.EmailAddress, new { id = "join-emailaddress", @class = "text", uiHintText = "Email address" })%>
<%= Html.ValidationMessageFor(c => c.EmailAddress, "*") %>
<%= Html.PasswordFor(c => c.Password, new { id = "join-password", @class = "text", uiHintText = "Password" })%>
<%= Html.ValidationMessageFor(c => c.Password, "*")%>
<%= Html.PasswordFor(c => c.PasswordConfirm, new { id = "join-password-confirm", @class = "text", uiHintText = "Password (repeat)" })%>
<%= Html.ValidationMessageFor(c => c.PasswordConfirm, "*")%>
<input type="submit" value="Sign me up!" class="submit" />
<% Html.EndForm(); %>
如果我發佈沒有輸入任何字段的表單,我始終會爲'ModelState.IsValid'獲取'true'的值。
它不應該是'假',因爲我已經將這些字段標記爲'必需'並且沒有輸入任何值?
我升級到ASP.NET MVC 2 RC 2 ,但我仍然有完全相同的問題。似乎升級改變了一件事情。 – Jonathan 2010-02-14 12:21:23
我認爲它現在已經修復了,但是一個奇怪的修復...在@ H_Sampat的回答下查看我的評論。 – Jonathan 2010-02-14 12:41:33