我有一個視圖的代碼,其中我從3個不同的文本框中提交一些數據。這只是我創建的練習示例。我在ASP.Net中使用MVC4的Razor語法。在ASP.Net MVC4驗證未綁定的文本框
我的問題:如何驗證文本框,以便始終需要它們?
@{
ViewBag.Title = "HelloWorld";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>HelloWorld</h2>
<div>This is a sample Hello World page</div>
<h1>@ViewBag.Title</h1>
<h2>@ViewBag.Message</h2>
@using (Html.BeginForm("HandleSubmit", "Home"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.Label("username", "UserName")
@Html.TextBox("username")
</li>
<li>
@Html.Label("pwd", "Password")
@Html.Password("pwd")
</li>
<li>
@Html.Label("cpwd", "Confirm Password")
@Html.Password("cpwd")
</li>
</ol>
<input type="submit" value="TestPost" />
<div style="color:red;font-weight:bold">@ViewBag.Feedback</div>
</fieldset>
}
UPDATE 1:的另一種方法來驗證文本框和顯示自定義無效消息是如下。
@Html.TextBox("username", null, new { @required = "required",
@oninvalid = "this.setCustomValidity('This data is a must')" })
MVC的模板是什麼你的意思是_unbound textboxes_?這些不是模型中的屬性? – 2014-08-28 04:02:13
它不受模型中的任何屬性限制。 – Sunil 2014-08-28 04:03:05