2

I am trying to populate the dropdown value with the selected value. I referred to related articles and I am doing the same. Still Dropdown is not populating with the selected Value. Not sure what I am missing.

Controller Code:

var RegionId = new[] { 
                new RegionKeyValues { RegionId = 1, RegionValue = "A" }, 
                new RegionKeyValues { RegionId = 2, RegionValue = "B" }, 
                new RegionKeyValues { RegionId = 3, RegionValue = "D" } 
            };
ViewData["RegionId"] = new SelectList(RegionId, "RegionId", "RegionValue", 1); 

View Code:

<%= Html.DropDownList("RegionId",
                      (SelectList)ViewData["RegionId"],
                      "-- Select --")%>
Alex
  • 34,899
  • 5
  • 77
  • 90
Rita
  • 1,237
  • 5
  • 24
  • 46
  • I don't think the question is clear. The drop down list doesn't populate the list with the contents of the RegionId list? Or you think that the list is supposed to preselect a value and it is not? – Zach Green Feb 08 '12 at 16:42
  • Dropdown is populating fine with those 3 records. But it is not setting to "A" – Rita Feb 08 '12 at 16:44
  • No.. It is not Dupe... There are couple of related... But My Dropdown is not setting to selected value of "A" that is 1. – Rita Feb 08 '12 at 16:45

3 Answers3

5

I think the problem is with the fact your property name is RegionId, as well as the ViewData name. Change one and see what happens.

Tr1stan
  • 2,755
  • 1
  • 26
  • 45
2

I'd try<%=Html.DropDownList("RegionId", new SelectList((IEnumerable)ViewData["RegionId"], "Value", "Text",1),"-- Select --")%>

Setting the value in the actual view.

Also in general I would create a strongly typed ViewModel and create properties storing the select list values as an IEnumerable<SelecListItem>

Then you can do

<%=Html.DropDownListFor(a=>a.RegionId, new SelectList(Model.Regions, "Value", "Text"),"-- Select --")`%>

Where RegionId would be the actual value to set to and Model.Regions stores the list of possible regions.

In general I feel a good approach to controllers and lists, viewmodels etc is in this post here

Community
  • 1
  • 1
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
1

Rita,

Here's my effort on this:

the model:

namespace MvcApplication1.Models
{
    public class RegionKeyValues
    {
        public int RegionId { get; set; }
        public string RegionValue { get; set; }
    }

    public class RegionKeyValuesViewModel
    {
        public int SelectedRegionId { get; set; }
        public IEnumerable<RegionKeyValues> RegionList { get; set; }
    }
}

Controller action:

public ActionResult Index()
{
    var regionList = new[] { 
        new RegionKeyValues { RegionId = 1, RegionValue = "A" }, 
        new RegionKeyValues { RegionId = 2, RegionValue = "B" }, 
        new RegionKeyValues { RegionId = 3, RegionValue = "D" } 
    };

    var viewModel = new RegionKeyValuesViewModel
    {
        RegionList = regionList, 
        SelectedRegionId = 1 // hardcoded here, but in real-life, from the db
    };

    ViewData.Model = viewModel;

    return View();
}

View code (in Razor):

@model MvcApplication1.Models.RegionKeyValuesViewModel
@{
    ViewBag.Title = "Home Page";
}

@Html.DropDownListFor(a => a.SelectedRegionId, 
    new SelectList(Model.RegionList, "RegionId", "RegionValue", 
        Model.SelectedRegionId), "-- Select --")

so basically, I'm passing a strongly typed ViewModel down to the view and setting the selected RegionId in the controller and passing that to Model.SelectedRegionId. In a real app, you'd obviously be getting the viewModel from the db or some other structure.

worth considering as an alternative.

jim tollan
  • 22,305
  • 4
  • 49
  • 63