<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ens.ContactPerson>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<%if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{ %>
<%= Html.ValidationSummary("Edit was unsuccessful . Please correct the errors and try again.")%>
<% using (Html.BeginForm())
{%>
<fieldset>
<legend>Fields</legend>
<p>
<%= Html.Hidden("Id", Model.Id)%>
<%= Html.ValidationMessage("Id", "*")%>
</p>
<p>
<label for="FirstName">FirstName:</label>
<%= Html.TextBox("FirstName", Model.FirstName)%>
<%= Html.ValidationMessage("FirstName", "*")%>
</p>
<p>
<label for="MiddleName">MiddleName:</label>
<%= Html.TextBox("MiddleName", Model.MiddleName)%>
<%= Html.ValidationMessage("MiddleName", "*")%>
</p>
<p>
<label for="LastName">LastName:</label>
<%= Html.TextBox("LastName", Model.LastName)%>
<%= Html.ValidationMessage("LastName", "*")%>
</p>
<p>
<label for="DateOfBirth">DateOfBirth:</label>
<%= Html.TextBox("DateOfBirth", String.Format("{0:g}", Model.DateOfBirth))%>
<%= Html.ValidationMessage("DateOfBirth", "*")%>
</p>
<p>
<label for="ContactPersonType">ContactPersonType:</label>
<%= Html.DropDownList("ContactPersonType")%>
<%= Html.ValidationMessage("ContactPersonType", "*")%>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "All")%>
</div>
<%} %>
</asp:Content>
我可以在这里应用一些javascript或jquery,这样在点击提交按钮后,它必须检查值是否有效,如果可以的话。
请提供验证姓氏字段的代码......这不应该是空的。
我尝试过这个功能,但是它没有工作......
protected void ValidateContact(ens.ContactPerson contactToValidate)
{
if (contactToValidate.FirstName.Trim().Length == 0)
ModelState.AddModelError("FirstName", "First name is required.");
if (contactToValidate.LastName.Trim().Length == 0)
ModelState.AddModelError("LastName", "Last name is required.");
if (contactToValidate.MiddleName.Trim().Length == 0)
ModelState.AddModelError("MiddleName", "Invalid phone number.");
if (contactToValidate.DateOfBirth.ToString().Trim().Length == 0)
ModelState.AddModelError("Email", "Invalid email address.");
}
答案 0 :(得分:0)
您将要使提交按钮成为常规按钮,然后单击调用验证字段的javascript函数。如果验证通过,您可以使用$(“#myForm”)。submit()提交表单。您可以执行var lastName = $(“#LastName”)。val()等操作来获取姓氏文本框中的值,然后测试lastName中的值。
答案 1 :(得分:0)
可能您可以使用Sys.Mvc.FormContext.getValidationForForm
强制验证整个表单。示例:http://weblogs.asp.net/imranbaloch/archive/2010/07/11/asp-net-mvc-client-side-validation-with-dynamic-contents.aspx
您需要一些JavaScript参考。请遵循此http://www.gregshackles.com/2010/02/validating-hidden-fields-in-asp-net-mvc-2/
之后,你可以在提交按钮上调用任何javascript方法,进行javascript验证,然后提交表单。
<script type="text/javascript" language="javascript">
function ValidateLastName(name) {
//all validation here
var form = document.forms[0];
form.action = '/AddressType/Create';
form.submit();
}
</script>
OR可能是其中之一可能有帮助,
http://forums.asp.net/t/1538157.aspx/1
My Own javascript validation + MicrosoftMvcValidation . Is it possible ? How