asp.net mvc xval validation
Posted
by mctayl
on Stack Overflow
See other posts from Stack Overflow
or by mctayl
Published on 2010-05-17T17:19:02Z
Indexed on
2010/05/17
17:40 UTC
Read the original article
Hit count: 270
Hi there I am using xval for the first time, it seems to work fine for required fields, However I am having some issues first of all it does not seem to validate booleans and also client validation is not working for me, this is not a major issue for me, the one that I really need to work is the stringlength property. It seems to do something because the form is not posted when the string length is exceeded, however no error message is displayed to the user which is obviously not what I want, has anyone been able to do this successfully?
My model goes like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace PitchPortal.Core
{
public class DocumentMetadata
{
//[Required]
// public bool visibility { get; set; }
[Required,StringLength(10, ErrorMessage = "title is too long")]
public string title { get; set; }
[Required, StringLength(10, ErrorMessage = "description is too long")]
public string description { get; set; }
[Required, StringLength(10, ErrorMessage = "summary is too long")]
public string summary { get; set; }
}
}
the html goes like this
<div id="results" title="Upload results"/>
<form id="myForm" action="<%=Url.Action("New") %>" method="post" enctype="multipart/form-data">
<% Html.EnableClientValidation(); %>
<%= Html.ValidationSummary() %>
<table>
<tr>
<td> <%=Html.Label("File")%></td>
<td>
<input type="file" id="file1" name="fileUpload" /> <br />
<%=Html.SubmitButton<DocumentController>(x => x.Upload(), "GetImage", "")%>
</td>
<td>
<%=Html.ValidationMessage("file1")%>
</td>
</tr>
<tr>
<td> <%=Html.Label("Visible")%></td>
<td>
<%= Html.RadioButton( "visibility",true,true) %>true
<%= Html.RadioButton("visibility", false)%>false
</td>
<td>
<%= Html.ValidationMessage("visibility")%>
</td>
</tr>
<tr>
<td> <%=Html.Label("Title")%></td>
<td> <%=Html.TextBox("doc.title")%></td>
<td> <%= Html.ValidationMessage("doc.title")%></td>
</tr>
<tr>
<td> <%=Html.Label("Description")%></td>
<td><%= Html.TextArea("doc.description")%></td>
<td><%= Html.ValidationMessage("doc.description")%></td>
</tr>
<tr>
<td> <%=Html.Label("Summary")%></td>
<td> <%= Html.TextArea("doc.summary")%></td>
<td> <%= Html.ValidationMessage("doc.summary")%></td>
</tr>
<tr>
<td> <%=Html.Label("Filetype")%></td>
<td> <%= Html.DropDownList("Filetype_id", (IEnumerable<SelectListItem>)ViewData.Model.AllFiletypesList)%> </td>
<td> <%= Html.ValidationMessage("doc.Filetype_id")%> </td>
</tr>
<tr>
<td> <%=Html.Label("Category")%></td>
<td><%= Html.DropDownList("cat.parent_id", (IEnumerable<SelectListItem>)ViewData.Model.AllCategoriesList, "-please select item-", new { className = "unselected" })%> </td>
<td><%= Html.ValidationMessage("cat.parent_id")%> </td>
</tr>
<%
if (Session["TempFolder"] == null)
{
for (int i = 1; i < 6; i++)
{ %>
<tr>
<td> <%=Html.Label("Shot "+i.ToString()) %> </td>
<td><input type="file" id="image_<%= i.ToString() %>" name="image_<%= i.ToString() %>" /></td>
</tr>
<% }
}%>
<tr>
<td><input type="submit" value="save"/></td>
</tr>
</table>
</form>
</div>
© Stack Overflow or respective owner