0

I get validation message always when I open this page (even first time), even if I choose value in one of dropdown lists, message doesn't go away. If I choose values in both I can submit form but messages still doesn't go away.

Snippet is Linq to sql class and LanguageID and SnippetTypeID are ints, I assume this happens because I pass empty model to View so LanguageID and SnippetTypeID are null and AFAIK Linq to Sql classes have required on non-nullable ints.

How can I fix this so validation messages doesn't appear before user tries to submit form, and if one of dropdown lists get selected to remove validation message.

View

@model Data.Snippet
@using (Html.BeginForm("Save", "Submit", FormMethod.Post))
{
    <h1 class="subtitle">Submit new snippet</h1>

    <h4>Title</h4>

    @Html.TextBoxFor(snippet => snippet.Title, new { @class = "form-field" })

    <h4>Language</h4>

    @Html.DropDownListFor(snippet => snippet.LanguageID, new SelectList(@ViewBag.Input.Languages, "ID", "Name", @Model.LanguageID), "Choose Language", new { @class = "form-select" })

    <p>@Html.ValidationMessageFor(snippet => snippet.LanguageID , "You must choose language", new { @class= "validation-message"})</p> 

    <h4>Snipet type</h4>

     @Html.DropDownListFor(snippet => snippet.SnippetTypeID, new SelectList(@ViewBag.Input.SnippetTypes, "ID", "Name", @Model.SnippetType), "Choose snippet type", new { @class = "form-select" })

     <p>@Html.ValidationMessageFor(snippet => snippet.SnippetTypeID,"You must choose snippet type", new { @class= "validation-message"})</p> 

     <h4>Text</h4>

     @Html.TextAreaFor(snippet => snippet.Text, new { cols = "20", rows = "10", @class = "form-field" })

     <input type="submit" value="Submit Snippet" />
}

Controller

        //Controllers are not finished Save() should have
        //code to actually insert to db after I fix validation
        // GET: /Submit/
        //
        public ActionResult Index()
        {
            Snippet model = new Snippet();

            SubmitModel input = new SubmitModel();

            ViewBag.Input = input;

            return View(model);
        }

        public ActionResult Save(Snippet snippet)
        {

            return View();
        }

Model

Model is Linq to Sql class.

Snippet
ID (int, identifier)
Title (string)
SnippetType (int, FK on table SnippetTypes)
LanguageID  (int, FK on table Languages)
Text (string)
formatc
  • 4,261
  • 7
  • 43
  • 81

1 Answers1

2

Alright,

So I think the reason it is failing is because of the custom CSS that you are adding. ValidationMessageFor will put a hidden class when the validation is successful.

if you want to add custom colors or something like that with you CSS i would consider applying the style to the wrapping p tag or adding a wrapping div/span and adding it to that.

You could probably define your messages on the view using only @Html.ValidationMessageFor(snippet => snippet.SnippetTypeID, "ErrorMessage"); However a more proper way is to take your Model and create Data Annotations for it.

Take a look at this article http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-annotation-validators-cs for more information about how to do model validation with Data Annotations.

Also I would consider passing in custom classes instead of your linq to sql class so that you can do custom validation based on the view. These custom classes are often refereed to as ViewModels.

Blast_dan
  • 1,135
  • 9
  • 18
  • You are genius, I would never suspect on css. I know about ViewModels and Data Annotations, but I just wanted to keep it as simple as I could(Lazy..). – formatc Jun 06 '12 at 15:12
  • No problem, Your problem stumped me for a bit there as well :) – Blast_dan Jun 06 '12 at 15:15