5

I'm trying to do the simplest thing with an dropdown and it's just not working. I have a an integer property called SearchCriteria.Distance. It's a simple integer property. I'm trying to bind that property to a drowpdown list of integers but it will not bind. The value is always 0. Here's the code:

@Html.LabelFor(x => x.SearchCriteria.Distance, "Radius (miles)", new { @class="control-label" })
                            <div class="controls">
                                @Html.DropDownListFor(x => x.SearchCriteria.Distance, new SelectList(new int[] { 5, 15, 25, 50 }), new { @class="input-small", style="height:36px;"})
                            </div>

Because it's a simple integer list, there's not Text Value to associate it with. What am I doing wrong here?

Edit: Turns out this problem was the result of a stupid error on my part. I had a hidden field with the SearchCriteria.Distance id on my form that I forgot about that prevented the drop down value from being set. I marked the solution below as the answer because it is correct.

3
  • The DropDownListFor helper will always render out a <select> list in HTML, which only takes string values. Your best bet is to create a new IEnumerable<SelectListItem> in a view-model, then pass this into your view and then reference it in your DropDownListFor. Commented Apr 15, 2013 at 8:17
  • That's what I tried at first, but it did not bind in that case either. Commented Apr 15, 2013 at 8:21
  • DropDownListFor accepts an IEnumerable of items and the OP's code will work - new SelectList(new int[] { 5, 15, 25, 50 }). @DonFitz can you post your controller code and view. Commented Apr 15, 2013 at 8:23

1 Answer 1

6

The example below should hopefully work enough for you to get an idea...

Controller ('/Controllers/TestController.cs' for this example):

public class TestController : Controller
{
    public ActionResult MyForm()
    {
        SearchViewModel model =
            new SearchViewModel()
            {
                Distance = 5 // This is the item that will be selected.
            };


        return View(model);
    }
}

View model (create a 'ViewModels' folder for your view models, this file would live in '/ViewModels/SearchViewModel.cs'):

public class SearchViewModel()
{
    public int Distance { get; set; }

    public IEnumerable<SelectListItem> DistanceItems
    {
        get
        {
            List<int> distances = new List<int>() { 5, 15, 25, 50 };

            return distances.Select(d => new SelectListItem() { Text = i.ToString(), Value = i.ToString() });
        }
    }
}

View ('/Views/Test/MyForm.cshtml'):

@model MvcApplication1.ViewModels.SearchViewModel

@Html.DropDownListFor(m => m.Distance, Model.DistanceItems)
Sign up to request clarification or add additional context in comments.

1 Comment

Now this makes perfect sense and I expect that this would work. I implemented as you have it here, but that SearchCriteria.Distance value is still not being set.

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.