Validation only happens during data binding. You're hand-copying all the values from the form into your objects, bypassing data binding (and validation).
Thq for u r help..i downloaded the project i.e in u gave me one link. i cannot able to run that project ...iam getting this error
d:\ASP.NET MVC\ASP.NET MVC Projects\MvcApplication5\MvcApplication5\Views\Shared\EditorTemplates\Customer.ascx(6): error CS0121: The call is ambiguous between the following methods or properties: 'System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor<Customer,string>(System.Web.Mvc.HtmlHelper<Customer>,
System.Linq.Expressions.Expression<System.Func<Customer,string>>, string)' and 'Microsoft.Web.Mvc.ExpressionInputExtensions.ValidationMessageFor<Customer,string>(System.Web.Mvc.HtmlHelper<Customer>, System.Linq.Expressions.Expression<System.Func<Customer,string>>,
string)'
thanks
asp.mvc2 validations
Mark as answer if this post help you .
Narsa Reddy
The version of MVC Futures that you're using isn't correctly matched up to the version of MVC you're using. Please download and use the correct version of MVC Futures.
Narsa
Member
105 Points
254 Posts
Edit validations in MVC? plz help
Dec 29, 2009 02:00 AM|LINK
Hi, i have done validations on create side, but when iam editing, the model state is true even textboxes are empty
so then it causes empty values saving into database.iam using mysql database.that's y i cannot use linqtosqlclasses.
i put total code for edit and create......plz help me on this...
thanks...
If Not ModelState.IsValid Thenhere if this condition returns true when textboxes are empty ,validation works fine
Return View() If Not ModelState.IsValid Then
Return View()
End If
End If
this is my entities for validations
#Region "Validation Entities"
<PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage:="The password and confirmation password do not match.")> _
Public Class PublisherCreate
Private _BusinessName As String
Private _ShortName As String
Private _RocNo As String
Private _UserLimit As Integer
Private _Website As String
Private _Title As String
Private _FirstName As String
Private _LastName As String
Private _LoginName As String
Private _Password As String
Private _ConfirmPassword As String
Private _DOB As Date
Private _JobTitle As String
Private _ICPpNo As Integer
Private _Sex As String
Private _PhoneMobile As Long
Private _PhoneOffice As Long
Private _Email As String
#Region "From Publisher Table"
<Required()> _
<DisplayName("Business Name")> _
Public Property BusinessName() As String
Get
Return _BusinessName
End Get
Set(ByVal value As String)
_BusinessName = value
End Set
End Property
<Required()> _
<DisplayName("Short Name")> _
Public Property ShortName() As String
Get
Return _ShortName
End Get
Set(ByVal value As String)
_ShortName = value
End Set
End Property
<Required()> _
<DisplayName("ROC NO")> _
Public Property RocNo() As String
Get
Return _RocNo
End Get
Set(ByVal value As String)
_RocNo = value
End Set
End Property
<Required()> _
<DisplayName("User Limit")> _
Public Property UserLimit() As Integer
Get
Return _UserLimit
End Get
Set(ByVal value As Integer)
_UserLimit = value
End Set
End Property
<DisplayName("Website")> _
Public Property Website() As String
Get
Return _Website
End Get
Set(ByVal value As String)
_Website = value
End Set
End Property
#End Region
#Region "From User_Profile Table"
<Required()> _
<DisplayName("Title")> _
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
<Required()> _
<DisplayName("First Name")> _
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
End Set
End Property
<DisplayName("Last Name")> _
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
<Required()> _
<DisplayName("Login name")> _
Public Property LoginName() As String
Get
Return _LoginName
End Get
Set(ByVal value As String)
_LoginName = value
End Set
End Property
<Required()> _
<ValidatePasswordLength()> _
<DataType(DataType.Password)> _
<DisplayName("Password")> _
Public Property Password() As String
Get
Return _Password
End Get
Set(ByVal value As String)
_Password = value
End Set
End Property
<Required()> _
<DataType(DataType.Password)> _
<DisplayName("Confirm Password")> _
Public Property ConfirmPassword() As String
Get
Return _ConfirmPassword
End Get
Set(ByVal value As String)
_ConfirmPassword = value
End Set
End Property
<DisplayName("DOB")> _
<DataType(DataType.Date)> _
Public Property DOB() As Date
Get
Return _DOB
End Get
Set(ByVal value As Date)
_DOB = value
End Set
End Property
<DisplayName("Job Title")> _
Public Property JobTitle() As String
Get
Return _JobTitle
End Get
Set(ByVal value As String)
_JobTitle = value
End Set
End Property
<DisplayName("ICPpNo")> _
Public Property ICPpNo() As Integer
Get
Return _ICPpNo
End Get
Set(ByVal value As Integer)
_ICPpNo = value
End Set
End Property
<DisplayName("Sex")> _
Public Property Sex() As String
Get
Return _Sex
End Get
Set(ByVal value As String)
_Sex = value
End Set
End Property
<DisplayName("Email")> _
<DataType(DataType.EmailAddress)> _
Public Property Email() As String
Get
Return _Email
End Get
Set(ByVal value As String)
_Email = value
End Set
End Property
<DisplayName("Phone Mobile")> _
Public Property PhoneMobile() As Long
Get
Return _PhoneMobile
End Get
Set(ByVal value As Long)
_PhoneMobile = value
End Set
End Property
<DisplayName("Phone Office")> _
Public Property PhoneOffice() As Long
Get
Return _PhoneOffice
End Get
Set(ByVal value As Long)
_PhoneOffice = value
End Set
End Property
#End Region
End Class
#End Region
This is my source code for Edit.......
<%@ Page Title="viaMedia" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(of ViaMediaMvc2.PublisherViewModel)" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div class="innerPL">
<h2 class="headerTxt">
Publisher Module <span>| Provide all the information</span></h2>
<div class="warning">
<%=Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.")%>
</div>
<div class="innerL">
<div class="formFld">
<h2>Edit</h2>
<br />
<% Using Html.BeginForm() %>
<table cellpadding="0" cellspacing="0" class="tabfld">
<tr>
<th>
Business Name:
</th>
<td>
<%=Html.TextBox("txtBusinessName", Model.Publishers.BusinessName)%>
<%=Html.ValidationMessage("txtBusinessName", "*")%>
</td>
</tr>
<tr>
<th>
Roc:
</th>
<td>
<%=Html.TextBox("txtRoc", Model.Publishers.ROCRobNo)%>
<%=Html.ValidationMessage("txtRoc", "*")%>
</td>
</tr>
<tr>
<th>
User Limit:
</th>
<td>
<%=Html.TextBox("txtUserLimit", Model.Publishers.UserLimit)%>
<%=Html.ValidationMessage("txtUserLimit", "*")%>
</td>
</tr>
<tr>
<th>
Website:
</th>
<td>
<%=Html.TextBox("txtWebsite", Model.Publishers.Website)%>
<%=Html.ValidationMessage("txtWebsite", "*")%>
</td>
</tr>
<tr>
<th>
Title:
</th>
<td>
<%=Html.TextBox("txtTitle", Model.UserProfiles.Title)%>
<%=Html.ValidationMessage("txtTitle", "*")%>
</td>
</tr>
<tr>
<th>
First Name:
</th>
<td>
<%=Html.TextBox("txtFirstName", Model.UserProfiles.FirstName)%>
<%=Html.ValidationMessage("txtFirstName", "*")%>
</td>
</tr>
<tr>
<th>
Last Name:
</th>
<td>
<%=Html.TextBox("txtLastName", Model.UserProfiles.LastName )%>
<%=Html.ValidationMessage("txtLastName", "*")%>
</td>
</tr>
</tr>
<tr>
<th>
Job Title:
</th>
<td>
<%=Html.TextBox("txtJobTitle", Model.UserProfiles.JobTitle )%>
<%=Html.ValidationMessage("txtJobTitle", "*")%>
</td>
</tr>
<tr>
<th>
IC No:
</th>
<td>
<%=Html.TextBox("txtICPpNo", Model.UserProfiles.ICPpNo )%>
<%=Html.ValidationMessage("txtICPpNo", "*")%>
</td>
</tr>
<tr>
<th>
Phone Mobile:
</th>
<td>
<%=Html.TextBox("txtPhoneMobile", Model.UserProfiles.PhoneMobile )%>
<%=Html.ValidationMessage("txtPhoneMobile", "*")%>
</td>
</tr>
<tr>
<th>
Phone Office:
</th>
<td>
<%=Html.TextBox("txtPhoneOffice", Model.UserProfiles.PhoneOffice )%>
<%=Html.ValidationMessage("txtPhoneOffice", "*")%>
</td>
</tr>
<tr>
<th>
Email:
</th>
<td>
<%=Html.TextBox("txtEmail", Model.UserProfiles.Email )%>
<%=Html.ValidationMessage("txtEmail", "*")%>
</td>
</tr>
<tr>
<th>
</th>
<td>
<input type="submit" class ="btn" value="Update" />
</td>
</tr>
</table>
<% End Using %>
</div>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</div>
<div class="innerR">
<div class="infoCnt infoCntTop">
<%--<h2>
List of advertisers</h2>
<p>
adResponse proprietary technology enables media buyers and agencies to intelligently
plan and execute media campaigns that do not rely on mere assumptions but will have
real measurable performance between all media channels.
</p>--%>
</div>
</div>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">
</asp:Content>
this is my Edit Controller for Edit.....
<AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal aintEntityId As Integer, ByVal collection As FormCollection) As ActionResult
Dim objPublisher As Publisher
Dim objUserProfile As UserProfile
Try
objPublisher = PublisherRepository.GetPublisherByEntityId(aintEntityId)
objUserProfile = userProfileRepository.GetUserProfileByEntityId(aintEntityId)
If Not ModelState.IsValid Then
Return View(New PublisherViewModel(objPublisher, objUserProfile))
End If
' TODO: Add update logic here
objPublisher.BusinessName = collection.Get("txtBusinessName")
'objPublisher.ShortName = model.Publishers.BusinessName
'objPublisher.PublisherType = 1
objPublisher.ROCRobNo = collection.Get("txtRoc")
objPublisher.UserLimit = collection.Get("txtUserLimit")
objPublisher.Website = collection.Get("txtWebsite")
objPublisher.ModifiedBy = 1
objPublisher.ModifiedDate = Now
PublisherRepository.UpdatePublisher()
'set values for user profile
objUserProfile.Title = collection.Get("txtTitle")
objUserProfile.FirstName = collection.Get("txtFirstName")
objUserProfile.LastName = collection.Get("txtLastName")
objUserProfile.JobTitle = collection.Get("txtJobTitle")
objUserProfile.ICPpNo = collection.Get("txtICPpNo")
objUserProfile.PhoneMobile = collection.Get("txtPhoneMobile")
objUserProfile.PhoneOffice = collection.Get("txtPhoneOffice")
objUserProfile.Email = collection.Get("txtEmail")
objUserProfile.ModifiedBy = 1
objUserProfile.ModifiedDate = Now
'UpdateModel(objPublisher)
'UpdateModel(objUserProfile)
userProfileRepository.UpdateUserprofile()
Return RedirectToAction("Index")
Catch
Return View(New PublisherViewModel(objPublisher, objUserProfile))
End Try
End Function
Controller for Create::::
<HttpPost()> _
Function Create(ByVal model As PublisherCreate) As ActionResult
Dim lobjEntity As New Entity
Dim lngentityId As Integer = 0
Dim lobjPublisher As Publisher
Dim intPublisher As Integer = 0
Dim lobjUserProfile As UserProfile
Dim intUserProfileId As Integer = 0
Try
If Not ModelState.IsValid Then
Return View()
End If
' TODO: Add insert logic here
'create entity
lobjEntity.EntityType = 1
lobjEntity.CreatedBy = 1
lobjEntity.CreatedDate = Now
lngentityId = entityRepository.AddEntity(lobjEntity)
'add Publisher info
If lngentityId > 0 Then
lobjPublisher = New Publisher
lobjPublisher.EntityID = lngentityId
lobjPublisher.BusinessName = model.BusinessName
lobjPublisher.ShortName = model.ShortName
lobjPublisher.UserLimit = model.UserLimit
lobjPublisher.ROCRobNo = model.RocNo
lobjPublisher.Website = model.Website
lobjPublisher.CreatedBy = 1
lobjPublisher.CreatedDate = Now
lobjPublisher.ModifiedBy = 1
lobjPublisher.ModifiedDate = Now
intPublisher = PublisherRepository.AddPublisher(lobjPublisher)
If intPublisher > 0 Then
lobjUserProfile = New UserProfile
lobjUserProfile.EntityID = lngentityId
lobjUserProfile.Title = model.Title
lobjUserProfile.FirstName = model.FirstName
lobjUserProfile.LastName = model.LastName
lobjUserProfile.LoginID = model.LoginName
lobjUserProfile.LoginPwD = model.Password
lobjUserProfile.Dob = model.DOB.ToShortDateString
lobjUserProfile.Email = model.Email
lobjUserProfile.JobTitle = model.JobTitle
lobjUserProfile.ICPpNo = model.ICPpNo
lobjUserProfile.Sex = CShort(Request("Sex"))
lobjUserProfile.PhoneMobile = model.PhoneMobile
lobjUserProfile.PhoneOffice = model.PhoneOffice
lobjUserProfile.CreatedBy = 1
lobjUserProfile.CreatedDate = Now
lobjUserProfile.ModifiedBy = 1
lobjUserProfile.ModifiedDate = Now
intUserProfileId = userProfileRepository.AddUserProfile(lobjUserProfile)
End If
End If
Return RedirectToAction("Index")
Catch
Return View()
End Try
End Function
Source code for Create::
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of ViaMediaMvc2.PublisherCreate)" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script language="JavaScript">
</script>
<div class="innerPL">
<h2 class="headerTxt">
Publisher Module <span>| Provide all the information</span></h2>
<div class="warning">
<%= Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.")%></div>
<div class="innerL">
<div class="formFld">
<div class="actions">
<ul class="topnav">
<li><a href="#">More actions</a>
<ul class="subnav">
<li><a href="#">Add event</a></li>
<li><a href="#">Edit</a></li>
<li><a href="#">Delete</a></li>
</ul>
</li>
</ul>
</div>
<h2>
Create</h2>
<br />
<% Using Html.BeginForm()%>
<table cellpadding="0" cellspacing="0" class="tabfld">
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.BusinessName)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.BusinessName)%>
<%=Html.ValidationMessageFor(Function(m) m.BusinessName)%>
</td>
</tr>
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.ShortName)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.ShortName)%>
<%=Html.ValidationMessageFor(Function(m) m.ShortName)%>
</td>
</tr>
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.RocNo)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.RocNo)%>
<%=Html.ValidationMessageFor(Function(m) m.RocNo)%>
</td>
</tr>
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.UserLimit)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.UserLimit)%>
<%=Html.ValidationMessageFor(Function(m) m.UserLimit)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.Website)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.Website)%>
<%=Html.ValidationMessageFor(Function(m) m.Website)%>
</td>
</tr>
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.Title)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.Title)%>
<%=Html.ValidationMessageFor(Function(m) m.Title)%>
</td>
</tr>
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.FirstName)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.FirstName)%>
<%=Html.ValidationMessageFor(Function(m) m.FirstName)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.LastName)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.LastName)%>
<%=Html.ValidationMessageFor(Function(m) m.LastName)%>
</td>
</tr>
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.LoginName)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.LoginName)%>
<%=Html.ValidationMessageFor(Function(m) m.LoginName)%>
</td>
</tr>
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.Password)%>
</th>
<td>
<%=Html.PasswordFor(Function(m) m.Password)%>
<%=Html.ValidationMessageFor(Function(m) m.Password)%>
</td>
</tr>
<tr>
<th>
<span># | </span><%=Html.LabelFor(Function(m) m.ConfirmPassword)%>
</th>
<td>
<%=Html.PasswordFor(Function(m) m.ConfirmPassword)%>
<%=Html.ValidationMessageFor(Function(m) m.ConfirmPassword)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.DOB)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.DOB)%>
<%=Html.ValidationMessageFor(Function(m) m.DOB)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.JobTitle)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.JobTitle)%>
<%=Html.ValidationMessageFor(Function(m) m.JobTitle)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.ICPpNo)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.ICPpNo)%>
<%=Html.ValidationMessageFor(Function(m) m.ICPpNo)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.Sex)%>
</th>
<td>
<%= Html.RadioButton("Sex", "1", Request("Sex")="1") %> Male
<%= Html.RadioButton("Sex", "2", Request("Sex")="2") %> Female
<%=Html.ValidationMessageFor(Function(m) m.Sex)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.PhoneMobile)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.PhoneMobile)%>
<%=Html.ValidationMessageFor(Function(m) m.PhoneMobile)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.PhoneOffice)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.PhoneOffice)%>
<%=Html.ValidationMessageFor(Function(m) m.PhoneOffice)%>
</td>
</tr>
<tr>
<th>
<%=Html.LabelFor(Function(m) m.Email)%>
</th>
<td>
<%=Html.TextBoxFor(Function(m) m.Email)%>
<%=Html.ValidationMessageFor(Function(m) m.Email)%>
</td>
</tr>
<tr>
<th>
</th>
<td>
<input type="submit" value="Create" class="btn" />
</td>
</tr>
</table>
<% End Using%>
</div>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</div>
<div class="innerR">
<div class="infoCnt infoCntTop">
<h2>
Why to create account?</h2>
<p>
adResponse proprietary technology enables media buyers and agencies to intelligently
plan and execute media campaigns that do not rely on mere assumptions but will have
real measurable performance between all media channels.
</p>
</div>
</div>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">
</asp:Content>
<div></div>
asp.net mvc2 validation
Narsa Reddy
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Edit validations in MVC? plz help
Dec 29, 2009 02:41 AM|LINK
Validation only happens during data binding. You're hand-copying all the values from the form into your objects, bypassing data binding (and validation).
ScottGu's blog post talks about how to make this work: http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-preview-1-released.aspx
Narsa
Member
105 Points
254 Posts
Re: Edit validations in MVC? plz help
Dec 29, 2009 08:56 AM|LINK
Hi,
Thq for u r help..i downloaded the project i.e in u gave me one link. i cannot able to run that project ...iam getting this error
d:\ASP.NET MVC\ASP.NET MVC Projects\MvcApplication5\MvcApplication5\Views\Shared\EditorTemplates\Customer.ascx(6): error CS0121: The call is ambiguous between the following methods or properties: 'System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor<Customer,string>(System.Web.Mvc.HtmlHelper<Customer>, System.Linq.Expressions.Expression<System.Func<Customer,string>>, string)' and 'Microsoft.Web.Mvc.ExpressionInputExtensions.ValidationMessageFor<Customer,string>(System.Web.Mvc.HtmlHelper<Customer>, System.Linq.Expressions.Expression<System.Func<Customer,string>>, string)'
thanks
asp.mvc2 validations
Narsa Reddy
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Edit validations in MVC? plz help
Dec 29, 2009 03:10 PM|LINK
The version of MVC Futures that you're using isn't correctly matched up to the version of MVC you're using. Please download and use the correct version of MVC Futures.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Edit validations in MVC? plz help
Dec 29, 2009 06:53 PM|LINK
Other Resources
<div>Using the New MVC 2 Templated Helpers</div><div>Walkthrough: Using Templated Helpers to Display Data</div>Narsa
Member
105 Points
254 Posts
Re: Edit validations in MVC? plz help
Dec 30, 2009 01:21 AM|LINK
Hi,
I am checking my MVC Features. In this link http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-preview-1-released.aspx u put one project so can u upload your project one again in to your blog because the products controllers and product model missed...
Thank You,
ASP MVC 2.0
Narsa Reddy