6
<div class="editor-label">
    @Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">

    @Html.EditorFor(model => model.Category)
    @Html.ValidationMessageFor(model => model.Category)
</div>

This gives me a label and a text box. How can I get a drop down list with static select items in place of the text box. Please help. I am new to ASP.NET MVC. I need solution / advice in Razor syntax.

1
  • 1
    @marc_s : I agree. It took me a while to register. It wont let me vote or accept answer until I registered and then there was a site error with OpenId. Didnt let me vote until now. Commented Jun 3, 2011 at 21:02

4 Answers 4

10
@Html.DropDownListFor( model => model.Category, new SelectList(new [] {"cat1", "cat2", "cat3"}) );
Sign up to request clarification or add additional context in comments.

Comments

3

Here is how you would populate a DropDownList using Model, View and Controller.

First the view

@using Website.Models
@model PageWithSelectList
@{
    ViewBag.Title = "Index";
}
@Html.DropDownList("DayOfWeek", Model.DaysOfWeek)

Then the Controller and Action method

using System.Web.Mvc;
using Website.Models;

namespace Website.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new PageWithSelectList();
            model.DayOfWeek = 3;
            return View(model);
        }
    }
}

and the HTML output

<select id="DayOfWeek" name="DayOfWeek">
<option value="1">Sunday</option>
<option value="2">Monday</option>
<option selected="selected" value="3">Tuesday</option>
<option value="4">Wednesday</option>
<option value="5">Thursday</option>
<option value="6">Friday</option>
<option value="7">Saturday</option>
</select>

I hope this helps.

Comments

0

Another way to fill your dropdownlist is by using the viewbag.

Controller:

public Action Create()
{
   FillViewBag();
   Return View();
}

private void FillViewBag()
    {
        List<SelectListItem> selectOptions = new List<SelectListItem>();            
        for(int i = 1; i <= 3; i++)
        {
            SelectListItem sli = new SelectListItem();
            sli.Text = "Option " + i.ToString();
            sli.Value = i.ToString();
            selectOptions.Add(sli)
        }
        ViewBag.SelectOptions = selectOptions;
     }

In your View:

 @Html.DropDownListFor(model => model.yourProp, ViewBag.SelectOptions as IEnumerable<SelectListItem>)

Hope this helps you!

Comments

0

You can use the foolowing code

Controler

var list = new SelectList(new[]
                                          {
                                              new {ID="1",Name="Employee"},
                                              new{ID="2",Name="Debtor"},
                                              new{ID="3",Name="Supplier"},
                                               new{ID="4",Name="Patient"},
                                          },
                           "ID", "Name", 1);
                ViewData["list"] = list;

                return View();

View

<div class="editor-field">

@Html.DropDownListFor(model => model.type,ViewData["list"] as SelectList,
 new { style = "width: 100px;"})

</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.