1

I create a dropdown list by using ASP.NET MVC helper like this,

<%=Html.DropDownList("Group","-please select a group-")%>

and I get the html like this,

<label for="GroupId">Group:</label>
<select id="GroupId" name="GroupId"><option value="">-please select a group-</option>
<option value="15">Business</option>
<option value="16">Friends</option>
<option value="17">Others</option>
</select>

The default option is "-please selecta group-" and the value is empty.

How can I validate the select value to see if it's empty? I mean if user doesn't choose a group, how can I know it and give user a error message. Now, the code only shows exception error, because the value is empty for the default option.

2 Answers 2

2

The value of Group in the Action will be 0. You could test if Group is zero and call ModelState.AddModelError. It would be better, if it was possible to explicitly set the value of the default item in the dropdownlist, but that is not possible in the Html.DropDownList.

Anyway I use the overload:

<%=Html.DropDownList("Group",Model.GroupList ,"-please select a group-")%>

With that you have one Property in the Model for the list of Groups and one for the key of the selected Group. If I use

<%=Html.DropDownList("Group",Model.GroupList ,"-please select a group-")%>

then ModelState.IsValid is always false for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thank you for your answer. But I could not test if Group is zero. How do u do it?
Hi Eric, Model.Group==0 if you use strongly typed view. (int)ViewData["Group"]==0 if you use ViewData as a Dictionary.
0

There are several approaches to accomplish this:

1. First approach: Custom validation

Model

namespace MyApplication.Models
{
    public class GroupModel
    {      
        public GroupList SelectdGroup { get; set; }
    }
 
    public enum GroupList
    {
       Business, Friends, Others
    }
}

View

@using MyApplication.Models
@model GroupModel

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    <b>Select Groupe: </b> 
 
    @Html.DropDownList("SelectdGroup", new SelectList(Enum.GetValues(typeof(GroupList))),"--Select Group--")
    @Html.ValidationMessage("SelectedGroup")
    <input type="submit" value="submit" />
}

Controller

using System.Web.Mvc;
using HtmlHelperDemo.Models;
 
namespace MyApplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {            
            return View();
        }
 
        [HttpPost]
        public ActionResult Index(GroupModel model)
        {           
            var selectedValue = model.SelectdGroup;
            
            if (selectedValue == "")
        {
           ModelState.AddModelError("SelectdGroup", "The Group is required");
        }
    
            
            return View();
        }
    }
}

2. Second approach: Using the attribute Required

Just add the “Required” attribute before the "SelectdGroup" field of the model and remove the check on the field "SelectdGroup" in the controller.

Model

namespace MyApplication.Models
{
    public class GroupModel
    {   
        [Required(ErrorMessage = "the Group is required.")]   
        public GroupList SelectdGroup { get; set; }
    }
 
    public enum GroupList
    {
        Business, Friends, Others
    }
}

Controller

using System.Web.Mvc;
using HtmlHelperDemo.Models;
 
namespace MyApplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {            
            return View();
        }
 
        [HttpPost]
        public ActionResult Index(GroupModel model)
        {           
            var selectedValue = model.SelectdGroup;                 
            
            return View();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.