1

In my ASP.NET MVC 4 application, I have a .dbml file (Model) created via LINQ to SQL. And I have:

Controller Action

public ActionResult Index()
        {
            MyDataContext db = new MyDataContext();
            ViewBag.Countries = db.Countries.ToList();

            return View();
        }

View

@model myProject.Models.Countries
<div>
@Html.DropDownListFor(m => m.CountryID, new SelectList(ViewBag.Countries, "CountryID", "CountryName"), "Select Country", new { id = "ddlCountries" })
</div>

The above dropdown list displays correctly with the names of the countries with the value as CountryID and the text as the Country name as follows:

Afghanistan
Albania
Algeria
...

...
Zambia
Zimbabwe

The database table Countries has other columns namely, "Capital" and "Population". I want to create custom data-attributes in the DropDownListFor for these two columns so that I can store data from the model in these columns and use that data in the textboxes for the Capital and the Population when a user selects a country in the dropdown. So, I modified the above view as follows:

@model myProject.Models.Countries
    <div>
    @Html.DropDownListFor(m => m.CountryID, new SelectList(ViewBag.Countries, "CountryID", "CountryName"), "Select CountryI", new { id = "ddlCountries", data_capital = Model.Capital, data_population = Model.Population })
    </div>

In Visual Studio 2012, the intellisense recognizes the Model.Capital and Model.Population. But in the browser, veiw gives the following error at the line where the above dropdown is created:

System.NullReferenceException: Object reference not set to an instance of an object.

The Countries table has no null values. It seems I cannot add the custom data-attributes to the SelectList. Moreover, the data_capital and data_population in the above view are in fact added to the select element - not to the option element. So what is a workaround?

Please help.

Thanks..Nam

nam
  • 21,967
  • 37
  • 158
  • 332
  • 1
    I am not aware of an easy way of adding more than 2 columns to a drop down list. I think the only way you can do that would be to create your own helper. I would recommend using an ajax call, sending the selected id and getting back the data you want. see my answer here http://stackoverflow.com/questions/19769483/mvc4-view-multiple-submit-buttons-update-label-text/19769631#19769631 – Matt Bodily Nov 24 '13 at 07:16
  • 2
    As a quick sanity check, do ALL countries have both Capital and population records. And you might want to create the select directly in mark-up and each option manually with a simple @foreach loop so that you can ensure that you assign the individual Country.Capital and Country.Popultation values to the individual data_ attributes rather than throwing a list of values at each attribute. – Code Uniquely Nov 24 '13 at 08:37
  • Per an advice from Code Uniquely I created the select directly in mark-up and each option manually with a simple @foreach loop on the Model data along with the individual data_attributes and it works. I suppose there is is by far the best choice. – nam Nov 26 '13 at 18:19

0 Answers0