title: MVC4 Validate Item Selected In DropDownList
body: <p>I have a page to create <code>objects</code>, and in this I have a <code>DropDownList</code>.  If I select an item from the list my page will save correctly, however if I don't select an item it looks like it fails on a postback as the objects will be null.</p>
  
  <p>What I want is to try and validate whether the user has selected an item (default is "Please Select...").</p>
  
  <p>I have code that will check and see in the controller if the item is null, but it's how do I then display a message?  Keeping all other details if they exist.</p>
  
  <pre><code>public ActionResult Create(int objectId = 0)
  {
  var resultModel = new MyObjectModel();
  resultModel.AllObjects = new SelectList(_system.GetAllObjects(objectId)); 
  // GetAllObjects juts returns a list of items for the drop down.
  return View(resultModel);
  }
  
  [HttpPost]
  public ActionResult Create(int? objectId, FormCollection collection)
  {
  try
  {
  int objectIdNotNull = 0;
  if (objectId &gt; 1)
  {
  objectIdNotNull = (int) objectId;
  }
  string objectName = collection["Name"];
  int objectTypeSelectedResult = 1;
  int.TryParse(collection["dllList"], out objectTypeSelectedResult);
  if (!Convert.ToBoolean(objectTypeSelectedResult))
  {
  // So here I have discovered nothing has been selected, and I want to alert the user
      return RedirectToAction("Create",
          new {ObjectId = objectIdNotNull, error = "Please select an Object Type"});
  }
  ....
  
  return RedirectToAction(...)
  
  }
  catch
  {
  return View();
  }
  }
  </code></pre>
  
  <p>The above code just goes to the Create page but doesn't display an error.  In my View for Create I have the following line which I assumed would display any errors:
       @ViewData["error"]</p></p>
  
  <p><em>Additional code</em>
  Model:</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Web.Mvc;
  using System.ComponentModel.DataAnnotations;
  
  namespace MyNameSpace
  {
  public class MyObjectModel
  {
  [Required(ErrorMessage = "Please select an Object Type")]
  public SelectList AllObjects { get; set; }  // I populate the drop down with this list
  }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@model MyNameSpace.MyObjectModel
  
  @{
  ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Create &lt;/h2&gt;
  
  &lt;p class="text-error"&gt;@ViewData["Message"]&lt;/p&gt;
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt; &lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt; &lt;/script&gt;
  
  @using (Html.BeginForm())
  { 
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;       
  &lt;div class="editor-label"&gt;
  @Html.LabelFor(model =&gt; model.MyObject.Name)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
  @Html.TextBoxFor(model=&gt;model.MyObjectType.Name, new {style="width: 750px"})
  @Html.ValidationMessageFor(model =&gt; model.MyObjectType.Name)
  &lt;/div&gt;
  
  &lt;div&gt;
  &lt;label for="ddlList"&gt;Choose Type&lt;/label&gt;
  @if (@Model != null)
  {  
  @Html.DropDownList("ddlList", Model.AllObjects, "Please Select...")
  @Html.ValidationMessageFor(model =&gt; model.AllObjects, "An object must be selected", new { @class = "redText"})
  }
  &lt;/div&gt;
  
  &lt;p&gt;
  &lt;input type="submit" value="Create" /&gt;
  &lt;/p&gt;
  &lt;/fieldset&gt;
  }
  </code></pre>