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 --")%>
3
  • 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? Commented Feb 8, 2012 at 16:42
  • Dropdown is populating fine with those 3 records. But it is not setting to "A" Commented Feb 8, 2012 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. Commented Feb 8, 2012 at 16:45

3 Answers 3

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.

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

Comments

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

Comments

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.

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.