MVC4 – Dropdown list example

Way 1 – this is not proper way to do this

        <div class="editor-field">
            @Html.DropDownListFor(model => model.Category, new SelectList(new [] {"Mobile phone", "Laptop", "Tablet"}))
            @Html.ValidationMessageFor(model => model.Category)
        </div>

Way 2 – We can implement this in more proper way:

        public ActionResult Create()
        {
            var categories = new SelectList(new[] { "Mobile phone", "Laptop", "Tablet" });
            ViewBag.categories = categories;
            return View();
        }

Create view:

        <div class="editor-field">
            @Html.DropDownListFor(model => model.Category, (SelectList)ViewBag.categories);
            @Html.ValidationMessageFor(model => model.Category)
        </div>