Frage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Test.Models;

namespace Test.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new DropDownListData();
            model.DropDownListItems.Add(new SelectListItem { Text = "Sun", Value = "1" });
            return View(model);
        }
    }
}

Says that

System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem> does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem> could be found (are you missing a using directive or an assembly reference?)

What reference am I missing?

War es hilfreich?

Lösung

As the error is trying to tell you, IEnumerable<T> is a read-only collection type.

If you want to mutate the collection, change it to List<T>.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top