-1

I have 2 dropdownlist whose values should be populated from database. I have checked the stored proc and its giving correct results. I have even debugged upto controller and it looks like I am getting right values.

I need help in figuring out the @Html.DropDownlistFor syntax. What i am using is incorrect. Any help?

Model:

  public class DropDownConfiguration
        {
            public string Project { get; set; }

            public string LineID { get; set; }

        }

Controller:

[ActionName("DetailsForm")]
        [HttpGet]
        public ActionResult DetailsForm()
        {
            try
            {
                IEnumerable<DropDownConfiguration> dropConfig = new List<DropDownConfiguration>();

                dropConfig = floorService.DropDownList();


                DetailsViewModel model = new DetailsViewModel()
             {

                 dropConfig = dropConfig,
             };
                return View("DetailsForm",model);

            }
            catch (Exception ex)
            {
                return View("_error");
            }

        }

Repository:

 public IEnumerable<DropDownConfiguration> DropDownList()
          {
              var model = new List<DropDownConfiguration>();

              using (var dbCmd = defaultDB.GetStoredProcCommand("dbo.GetLineIDProject"))
              {

                  var varCmd = defaultDB.ExecuteReader(dbCmd);

                  while (varCmd.Read())
                  {
                      model.Add(new DropDownConfiguration()
                        {
                            LineID = varCmd.IsDBNull(0) ? "" : varCmd.GetString(0),
                            Project = varCmd.IsDBNull(1) ? "" : varCmd.GetString(1),
                            Quarter = varCmd.IsDBNull(2) ? 0 : varCmd.GetInt32(2),
                            Year = varCmd.IsDBNull(3) ? 0 : varCmd.GetInt32(3)

                        });

                  }
              }
              return model;
          }

Html (Here are the 2 dropdown list LineID, Project)-----NEED HELP HERE------

@Html.DropDownListFor(m => m.dropConfig.FirstOrDefault().LineID, "--Select LineID--", new { @class = "form-control" })

@Html.DropDownListFor(m => m.dropConfig.FirstOrDefault().Project, "--Select Project--", new { @class = "form-control" })

On debugging @Controller: enter image description here

beginner
  • 303
  • 1
  • 10
  • 31
  • Your view makes no sense. A dropdownlist requires 2 properties - one to bind to and one for the options to be displayed. Your model does not contain properties to bind to. Suggest you look at [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for generating a dropdownlist –  Nov 13 '17 at 04:24
  • I think your `DropDownListFor` setting is wrong. Why using `FirstOrDefault()` to bind with selected value? Also you're not using `IEnumerable` to populate the DDL at second argument as data collection list - what exactly you want to do? – Tetsuya Yamamoto Nov 13 '17 at 04:24
  • Oh ok. Lemme check on this. – beginner Nov 13 '17 at 04:31
  • @StephenMuecke LineID and Project are two different fields with separate DropDownlist. If I use LineID as binding, can I use both LineID and Project as separate DropDown? – beginner Nov 13 '17 at 05:29
  • Sorry, but your code makes no sense so its not clear what your trying to do or to actually bind to. –  Nov 13 '17 at 05:32
  • @StephenMuecke Please have a look at the image I have updated in the question. Its from debugging. I am getting each rows from database in array fashion. from them I need display LineID & Project as 2 separate dropdown. Hope I am clear now – beginner Nov 13 '17 at 05:45
  • Sorry, but its still not making any sense. Have a look at the link I gave you to understand how to generate and bind to a dropdownlist. –  Nov 13 '17 at 05:51

1 Answers1

0

What you need to do, is to create a new select list.

  public class DropDownConfiguration
        {
            public string Project { get; set; }

            public string LineID { get; set; }

            public SelectList dropDownSelectList{ get; set; }

        }

[ActionName("DetailsForm")]
        [HttpGet]
        public ActionResult DetailsForm()
        {
            try
            {
             DropDownConfiguration dropConfig = new DropDownConfiguration();

       IEnumrebale<DropDownConfiguration> selectList = floorService.DropDownList();


                DetailsViewModel model = new DetailsViewModel()
             {

                 dropConfig = selecrList ,
             };
                dropConfig.dropDownSelectList  = new SelectList(selectList, "Value", "Text");
                return View("DetailsForm",model);

            }
            catch (Exception ex)
            {
                return View("_error");
            }

        }

Your View:

@Html.DropDownListFor(m => m.LineID, Model.dropDownSelectList, "--Select LineID--", new { @class = "form-control" })

Your code is unclear. Controller model and repository is not written properly, I have no idea why you have separated the model and create repository class (as business logic should be in the model. There is no clue as to what your DetailsViewModel includes and it seems you have no use for it either.

And when you user first of default in your view the way you did, it means that when you submit the form, you will always get the value of the first field or null, as you didn't populate any values and furthermore if your aim was to populate the drop down list, you would have populated only one field.

Review your code, make the necessary changes as I have shown in the snippet and make sure your code is clear.

Barr J
  • 10,636
  • 1
  • 28
  • 46