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)