1

I am creating an ASP.Net MVC 4 site where I have a controller with a create action.

The Create action provides a View with several dropdowns each of which I want to bind to a single list of entities from my database.

However, I want to ensure that the selections in each drop down are unique.

How would I go about doing this? Do I need to do some sort of AJAX postback to the server each time a selection is made to somehow update the available options on each of the other drop downs? Or is there a simpler way?

Thanks very much,

Martyn Jones.

4
  • 1
    well if you want to avoid errors "before validation", than you'll have to use Ajax (your scenario) or (maybe easier) jQuery : event on each dropDown change, removing options with same value as the selected one on all other dropDown. By the way I would add "server-side" validation before saving to db. Commented Mar 21, 2013 at 16:49
  • @RaphaëlAlthaus Thank you very much. When you say server side validation, would you just carry that out in the controller actions before saving to the database, or is there a way of applying the constraints I want at the model level? Thanks again! Commented Mar 21, 2013 at 18:21
  • 1
    Well, if I were you (but I'm not), I would take a look at FluentValidation (fluentvalidation.codeplex.com), really nice for readable and wide validation rules Commented Mar 21, 2013 at 18:23
  • @RaphaëlAlthaus That looks like exactly the sort of thing I was looking for. Thanks very much! Commented Mar 21, 2013 at 18:31

1 Answer 1

0

In your model you can have individual Id fields for each Dropdown. something as below

<%: Html.DropDownListFor(model => model.DropwdownId1,Model.DropdownEntities) %>
<%: Html.DropDownListFor(model => model.DropwdownId2,Model.DropdownEntities) %>
<%: Html.DropDownListFor(model => model.DropwdownId3,Model.DropdownEntities) %>

where Dropdownentities will just return entities from database.

you shall be able to see selected values for each dropdown in Post Action. DropwdownId1 , DropwdownId2

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

2 Comments

Thanks for your answer. Would the fact that each dropdown was using a single source, prevent the same selection being made in multiple dropdowns?
No it will not. selection will be saved in ID. you could have same selection across multiple doropdowns or multiple selection across multiple dropdowns

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.