0

I'm trying to return a list of emails from a model using DropDownListFor. I have found a few topics on this issue, so it seems to be common, and I presume the list needs to be repopulated, but I can't find a way that works (or a way that I understand against the other examples).

I always get the null error regardless of what I do. I'm thinking that maybe actually it is not returning any emails in the first place, but I'm not understanding the way dropdownlist works with a model.

Is there a different issue causing the null that I can't see right now?

I define the list in a class:

public class User {
    [Required]
    [Display(Name = "Email Address")]
    public string Email { get; set; }
    public IEnumerable<SelectListItem> EmailList { get; set; }
}

Controller:

[HttpGet]
public ActionResult ChangeDetails()
{
    var u = new User();
    ViewBag.DropDownList = new SelectList(u.EmailList, "Email", "Email");
    return View(u);
}

View:

@Html.DropDownListFor(model => model.Email, (IEnumerable<SelectListItem>)ViewBag.DropDownList, "---Select a value---", new { htmlAttributes = new { @class = "form-control" } })

Error message:

Message: Value cannot be null. Parameter name: items

at System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, String dataGroupField, IEnumerable selectedValues, IEnumerable disabledValues, IEnumerable disabledGroups) ....
8
  • try passing "ViewBag.DropDownList" in the SelectList inside your razor view, rather than Model.EmailList Commented Sep 18, 2017 at 10:29
  • @Nirman I actually had that in and changed it to new SelectList ... trial and error really. Edited the main post to add the ViewBag in razor. Commented Sep 18, 2017 at 10:31
  • Because u.EmailList is null (you did not initialize it, let alone populate it with any items) Commented Sep 18, 2017 at 12:24
  • @Stephen Muecke How can I populate it with items? Been trying to wrap the answer (lst) below in a foreach and doing SelectListItem item in user.EmailList, but to no avail. I feel like I'm not understanding something. Commented Sep 18, 2017 at 13:13
  • But all you have done is var u = new User(); which just initializes a new User which wont have any items. And you do not even need ViewBag.DropDownList = ... The model already contains a property IEnumerable<SelectListItem> EmailList so in the view its just @Html.DropDownListFor(model => model.Email, Model.EmailList, "---Select a value---", new { ... }) (except that you must first initialize EmailList so its not null) Commented Sep 18, 2017 at 13:26

2 Answers 2

3

Try this as SelectListItem:

IList<SelectListItem> lst = new List<SelectListItem>();
        lst.Add(new SelectListItem()
            {
               Value = "Hello",
                Text = "World"
            });        
ViewBag.DropDownList = lst;

Or:

var u = new User();
ViewBag.DropDownList = u.EmailList //You need to populate your EmailList first before you declare it here.
return View(u);

Another option is this:

IList<SelectListItem> lst = new List<SelectListItem>();
        lst.Add(new SelectListItem()
            {
               Value = "Email",
               Text = "Email"
            });            
ViewBag.DropDownList = new SelectList(p.EmailList, "Value", "Text");

and on your View:

@Html.DropDownListFor(model => model.Email, (SelectList)ViewBag.DropDownList, "---Select a value---", new { htmlAttributes = new { @class = "form-control" } })
Sign up to request clarification or add additional context in comments.

2 Comments

The first solution just returns one record World whereas it should return a list of emails. Have I missed the part where I should be looking/querying for them? I thought the model is able to resolve it automatically.
Of course it's just my example, you need to populate that with your desired record. I just give you lead on how you it should work and prevent the error.
0

ensure that you are passing your selectlist in both get and post methods

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.