0

I am trying to create a dropdonw in my MVC web application.

Model

namespace projectname.Models 
{
public class DropDownModel
    {
         public int id{get; set;}
          puclic string value {get; set;}
    }
}

Controller

using projectname.Models;
{
public class DropDownController: Controller
    {
         public ActionResult Index()   //where Index is one of the view
         {
               List <SelectListItem> listItem = new List<SelectListItem>();
               DropDownModel drop = new DropDownModel();
                drop.id = 1;
                drop.value  = "First";

                listItem.Add(new SelectListItem() {Value = drop.Value, Text = drop.id.toString()});

                return view(listitem);
         }

    }

}

View

@{
ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>

<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.

</p>

However, the drop down is not being displayed on my Index view.

3 Answers 3

1

I would suggest reading more about MVC. You have nothing rendering the dropdown on your view and you have a model that more or less does the same-thing your listitem is doing. This could be handled by one object instead of two. That said :

Controller

  public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            DropDownModel drop = new DropDownModel();
            drop.id = 1;
            drop.value = "First";

            listItem.Add(new SelectListItem() { Value = drop.id.ToString(), Text = drop.value });


            return View(listItem);
        }

    }

View Note the @Model List at the top of the view. This defines the strongly typed model asigned to the view. This Model is passed from the controller (listitem) to the view.

@model List<SelectListItem>

@{
    ViewBag.Title = "title";
}

@Html.DropDownList("name", Model)

<h2>title</h2>
Sign up to request clarification or add additional context in comments.

Comments

0

There are a few ways of display DropDownList in MVC. Here is my way.

Note: You need a collection of SelectListItem in model.

Model

public class MyModel
{
    public int SelectedId { get; set; }
    public IList<SelectListItem> AllItems { get; set; }

    public MyModel()
    {
        AllItems = new List<SelectListItem>();
    }
}

Controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyModel();
        model.AllItems = new List<SelectListItem>
        {
            new SelectListItem { Text = "One",  Value = "1"},
            new SelectListItem { Text = "Two",  Value = "2"},
            new SelectListItem { Text = "Three",  Value = "3"}
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyModel model)
    {
        // Get the selected value
        int id = model.SelectedId;
        return View();
    }
}

View

@model DemoMvc.Controllers.MyModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.DropDownListFor(x => x.SelectedId, Model.AllItems)
    <input type="submit" value="Submit" />
}

enter image description here

3 Comments

Thank You! Exactly what I wanted. Also, shouldn't it be @model DemoMvc.Models.Mymodel in the view?
He most lkely has @model DemoMvc.Controllers.MyModel in his view because he declared his model in the controller (to save time for his sample) Yours should be model DemoMvc.Models.Mymodel
@nitinsh99 Yes, MyModel should be inside Models folder.
0

You need to give your View the Model which is the listItem.

return View(listItem);

2 Comments

Hi, I tried it didn't help. Do I need to make any changes in the View itself? and You said model..Did you mean drop instead of listitem?
@nitinsh99 Yes you would have to, but check out the answer by PaulBinder. His example looks like it would work.

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.