1

This is part of a .NET MVC project.

I have a DropDownList that is populated by an array of strings from my model. The actual list functions fine and populates correctly, but when an item is selected, there is no value passed. The value should just be equal to the text being selected from the dropdownlist.

I can see that it likely isn't assigned anywhere, but I'm relatively inexperienced with MVC/HTML projects.

        <div class="form-group">
            @Html.LabelFor(model => model.Cores, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Cores", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Cores, "", new { @class = "text-danger" })
            </div>
        </div>

The code I'm using is modified from a larger project that I've (apparently) taken over, so I'm learning as I go with this.

EDIT:

This is how the list of cores is initialized and passed to the view page.

var cores = db.Cores.ToList();
            var coreName = new List<string>();

            foreach (Core core in cores)
            {
                coreName.Add(core.Name);
            }

            ViewBag.Cores = new SelectList(coreName);

            return View();
Lennac
  • 186
  • 1
  • 2
  • 16
  • This is a possible duplicate of "Get the selected value of a DropDownList. Asp.NET MVC" https://stackoverflow.com/questions/15881575/get-the-selected-value-of-a-dropdownlist-asp-net-mvc – Yves Rochon Mar 19 '20 at 13:02
  • What is the controller action signature? In the browser's debugging tools, what is the `name` of the resulting ` – David Mar 19 '20 at 13:02

1 Answers1

2

You're just creating a dropdown which is not binded to your model. You should use Html.DropDownListFor, not Html.DropDownList.

public class MyModel {
   public string Core { get; set; }
}

In your view file,

@Html.DropDownListFor(n => n.Core, (SelectList)ViewBag.Cores, htmlAttributes: new { @class = "form-control" })

And post action in your controller

[HttpPost]
public ActionResult Report(MyModel model)
{
   //model.Core is selected core.
}

UPDATE:

If you don't have a Model,

In your view file,

@Html.DropDownList("Core", (SelectList)ViewBag.Cores, htmlAttributes: new { @class = "form-control" })

And post action in your controller

[HttpPost]
public ActionResult Report(string Core)
{
   //Core is the selected one.
}
Community
  • 1
  • 1
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • Thanks for the reply. I should have included this in my original question, but my "Cores" variable is initialized through a call to the database. I've edited my question to include how the model is prepared in the Controller. – Lennac Mar 19 '20 at 13:36
  • @Lennac, It's not important how you populate your Cores, you can also use ViewBag. It's about how you get selected item from dropdown. I updated my answer. – Okan Kocyigit Mar 19 '20 at 13:40
  • I get an error saying HtmlHelper doesn't contain a definition for DropDownListFor (this occurred when started adding ViewBag.Cores instead of "new SelectList(model.Cores)". If I keep "new SelectList(ViewBag.Cores)" the list loads, but only as the object type "Mvc.SelectListItem" rather than the actual text value. Likewise, if I use your final example, it says that HtmlHelper has no applicable method named "DropDownList" – Lennac Mar 19 '20 at 13:58
  • Yes, you're right.`ViewBag.Cores` must be cast to `SelectList`, updated my answer. – Okan Kocyigit Mar 19 '20 at 14:02
  • That did it! Thank you so much for your help. I think the confusion (for me) is that on my view page I don't have the model, rather it's using the ViewBag that is initialized. The post action takes the values and creates the model (which then writes to the db). Again, thank you so much. – Lennac Mar 19 '20 at 14:04