Reputation: 2724
I have a dropdown list in my MVC application which is a required field in my form.
I have created and filled out my list like so:
@Html.DropDownListFor(x => x.ServiceName,
new SelectList(
new List<Object>
{
new { value = "Empty", text = "Select..."},
new { value = "Service Name 1", text = "Service Name 1"},
new { value = "Service Name 1", text = "Service Name 2"},
new { value = "Service Name 1", text = "Service Name 3"},
new { value = "Service Name 1", text = "Service Name 4"}
},
"value",
"text",
0), new { @class = "form-control" })
I'm needing my form to return back a message that tells the user that they need to select an option from this menu is they do no select anything. My service name has [Required(ErrorMessage = "Service Name must be selected")]
above it in my Model class.
It looks like this:
[Required(ErrorMessage = "Service Name must be selected")]
public string ServiceName { get; set; }
However I have no idea how I can check against this value. So far I've tried the following:
if(newMember.getProperty("serviceManager").Value == "0")
{
ModelState.IsValid = false;
}
But this doesn't work. I've tried a few variations of the above but they haven't worked either. Im currently googling to find an answer but I'm not having much luck.
Could someone please explain to me how I can achieve what I want to achieve?
Edit
Just updated my code to show the new test I just completed but it still didn't return back an error telling me Service must be selected when I submit an empty form.
Upvotes: 1
Views: 2153
Reputation:
Use the overload of DropDownListFor()
that renders an option label, and make you ServiceName
property nullable with [Required]
attribute. The issue with you existing SelectList
is that you include the first option which has a value ("Empty") which is a valid string so it passes validation.
Model
[Required]
public string ServiceName { get; set; }
Controller
List<string> services = new List<string>() { "Service Name 1", "Service Name 2", "Service Name 3", "Service Name 4" };
ViewBag.Services = new SelectList(services);
return View(yourModel);
View
@Html.DropDownListFor(x => x.ServiceName, (SelectList)ViewBag.Services, "--Please Select--", new { @class = "form-control" })
@Html.ValidationMessagFor(x => x.ServiceName)
Note the SelectList
does not contain an element for "--Please Select--". The 3rd parameter of DropDownListFor()
adds a label option which has no value <option value>--Please Select--</option>
which if selected will result in the property being invalid.
Upvotes: 1
Reputation: 252
You have your getProperty to get serviceManager but you view is binding to ServiceName.
Try changing it to...
if(newMember.getProperty("ServiceName").Value == "0")
OR
I dont have visual studio available at the minute so code might not be exact.
@Html.DropDownListFor(x => x.ServiceName,
new SelectList(
new List<Object>
{
new { value = 1, text = "Service Name 1"},
new { value = 2, text = "Service Name 2"},
new { value = 3, text = "Service Name 3"},
new { value = 4, text = "Service Name 4"}
},
"value",
"text",
0), "Select...", new { @class = "form-control" })
and if you have [Required] on your ServiceName in your ViewModel then in your controller you will be able to do ModelState.IsValid e.g.
if(ModelState.IsValid){
// Do code here if it is valid
}else{
return View(viewModel)
}
Upvotes: 1