?
Solved

Required Error Message is not displaying as expected on a field that is a Dropdown List

Posted on 2014-03-24
4
Medium Priority
?
328 Views
Last Modified: 2014-04-09
I have the following metadata for my model (just showing the one data table):

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; }
    }

Open in new window


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);
        }

Open in new window


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>
}

Open in new window


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!
0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 39954244
try something like this

[Required(ErrorMessage = "* Server Type is required")]
[Range(0, int.MaxValue, ErrorMessage = "MESSAGE HERE")]
public int databaseServerTypeID { get; set; }

Open in new window


The property is of type int, you have to use a range as above to trigger the validation
0
 

Author Comment

by:dyarosh
ID: 39955981
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.
0
 

Accepted Solution

by:
dyarosh earned 0 total points
ID: 39978423
I found the problem.  I had to change from DropDownList to DropDownListFor.  Now my error message displays if an option is not selected from the DropDownList before posting back to the server.
0
 

Author Closing Comment

by:dyarosh
ID: 39988232
Found the solution on my own.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question