dyarosh
asked on
Required Error Message is not displaying as expected on a field that is a Dropdown List
I have the following metadata for my model (just showing the one data table):
I am using the built-in [Required] attribute.
Here are my Create actions in my controller:
Here is a partial listing of my View:
The problem is when I click Create without entering any data, I get the Required Error Messages on the Database Name field and Description Field but not the Server Type Field which is showing -- Choose Server Type -- .
When I enter a database name and description and hit Create, the Required message for the Server Type is displayed.
How do I get the Server Type Required message to display if -- Choose Server Type -- is displayed when Create is clicked?
Any help is greatly appreciated!
public class AppCatalog_DatabaseMetadata
{
[UIHint("MVC4PrimaryKey")]
[DisplayName("ID")]
public int databaseID { get; set; }
[HtmlProperties(Size = 100, MaxLength = 100)]
[Required(ErrorMessage = "* Database Name is required")]
[DisplayName("Database Name")]
[UIHint("MVC4String")]
public string databaseName { get; set; }
[DisplayName("Description")]
[UIHint("MVC4TextBox")]
[HtmlProperties(Rows = 5, Cols = 75, MaxLength = 1000)]
[Required(ErrorMessage = "* Description is required")]
public string databaseDescription { get; set; }
[DisplayName("S1")]
[UIHint("ServerType")]
[Required(ErrorMessage = "* Server Type is required")]
public int databaseServerTypeID { get; set; }
public virtual AppCatalog_ServerType AppCatalog_ServerType { get; set; }
}
I am using the built-in [Required] attribute.
Here are my Create actions in my controller:
public ViewResult Create()
{
ViewBag.Title = "Application Catalog -> Database Catalog -> Add Database";
SelectList slServerType = new SelectList(db.AppCatalog_ServerType, "servertypeID", "servertypeName");
ViewBag.databaseServerTypeID = slServerType;
return View();
}
//
// POST: /DBCatalog/Create
[HttpPost]
public ActionResult Create(AppCatalog_Database appcatalog_database)
{
if (ModelState.IsValid)
{
//db.AppCatalog_Database.Add(appcatalog_database);
//db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.databaseServerTypeID = new SelectList(db.AppCatalog_ServerType, "servertypeID", "servertypeName", appcatalog_database.databaseServerTypeID);
return View(appcatalog_database);
}
Here is a partial listing of my View:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "", new { @class = "validation-summary-errors" })
<fieldset>
<div class="editor-field">
@Html.Label("Server Type", new { @class = "editor-label" }) :
@Html.DropDownList("databaseServerTypeID", "-- Choose Server Type --")
<span class="error-msg">@Html.ValidationMessageFor(model => model.databaseServerTypeID)</span>
</div>
<div class="editor-field">
@Html.EditorFor(model => model.databaseName)
<span class="error-msg">@Html.ValidationMessageFor(model => model.databaseName)</span>
</div>
<div class="editor-field">
@Html.EditorFor(model => model.databaseDescription)
<span class="error-msg">@Html.ValidationMessageFor(model => model.databaseDescription)</span>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
The problem is when I click Create without entering any data, I get the Required Error Messages on the Database Name field and Description Field but not the Server Type Field which is showing -- Choose Server Type -- .
When I enter a database name and description and hit Create, the Required message for the Server Type is displayed.
How do I get the Server Type Required message to display if -- Choose Server Type -- is displayed when Create is clicked?
Any help is greatly appreciated!
ASKER
I tried your suggestion but still couldn't get the required error message to display. I even changed the range to be 1 to int.MaxValue.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Found the solution on my own.
Open in new window
The property is of type int, you have to use a range as above to trigger the validation