0

I making a MVC2 project

View:

<%= Html.DropDownListFor ( m => m.CategoryId, Model.CategoryList )%>
 <%= Html.ValidationMessageFor ( m => m.CategoryId )%>
  <br />
  <%if ((Html.TextBoxFor(m => m.NewCategory)).Equals("")) %>
    <%{ show error msg near the textbox "* field is required"}%>
     <% else %>
   <%= Html.TextBoxFor(m => m.NewCategory)%>

If textbox is empty then show error msg and let the user to input again, how to do this?? :S

user3021004
  • 15
  • 1
  • 4

1 Answers1

1

put a " [Required]" condition to the Property of your Model.

public class MyModel {
   [Required]
   public string NewCategory { get; set; }
}

A complete explained example can be found there: http://www.codeproject.com/Articles/220025/A-sample-on-Asp-Net-MVC-Model-Validation-using-Dat

Ole Albers
  • 8,715
  • 10
  • 73
  • 166