?
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
?
321 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

800 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