1

Ok so I got my dropDown box going, I have my options and if I select any, I want to write the submitted selected item's value, sounds easy right? well every time I click any option and submit it, the value is displayed on the URL, but I cant seem to write it out...

View (Index.cshtml):

<p>Dropdown Using viewbag with Html.DropDownList </p>
<br/>
<div>
        @using (Html.BeginForm("dropDown", "Home", FormMethod.Get))
    {
        @Html.DropDownList("ListItem")

        <button type="submit"></button>
    }

</div>

<p>@ViewBag.SelectedValue</p> //should print the value I selected but it does nothing...

Controller (HomeController.cs):

public ActionResult Index()
    {
        List<SelectListItem> ObjItem = new List<SelectListItem>()
        {
      new SelectListItem {Text="Select",Value="0",Selected=true },
      new SelectListItem {Text="ASP.NET",Value="1" },
      new SelectListItem {Text="C#",Value="2"},
      new SelectListItem {Text="MVC",Value="3"},
      new SelectListItem {Text="SQL",Value="4" },
        };
        ViewBag.ListItem = ObjItem;

        return View("Index");
    }

    public ActionResult dropDown(List<SelectListItem> ListItem)
    {
        ViewBag.SelectedValue = ListItem[0].Value; //crossing fingers this has the value I want to print


        return this.View("Index");
    } 
  • A ` –  May 19 '16 at 04:40
  • You can use Jquery to write the selected value to screen. This post can help you about writing selected item to screen. http://stackoverflow.com/questions/2780566/get-selected-value-of-a-dropdowns-item-using-jquery – oneNiceFriend May 19 '16 at 04:42
  • @oneNiceFriend not looking for js solutions –  May 19 '16 at 04:47
  • @StephenMuecke hmm can you copy/paste this code to your project and find a working solution, then answer this question please? ive been trying to get this done all day, thanks –  May 19 '16 at 04:48
  • @FluffyWuffy, You really need to study the code that Shyju gave your [last question](http://stackoverflow.com/questions/37304543/how-to-mvc-5-drop-down-multiple-select-box) - the only difference is that it will be `public int SelectedOption { set; get; }` rather than `public string[] SelectedOptions { set; get; }`. What you doing here is awful code (as is the answer you accepted) –  May 19 '16 at 06:32

3 Answers3

2

try like this

View (Index.cshtml):

<div>
    @using (Html.BeginForm("dropDown", "Home", FormMethod.Post))
    {
        @Html.DropDownList("ListItem")

        <button type="submit">Submit</button>
    }

</div>

<p>@ViewBag.SelectedValue</p>

Controller (HomeController.cs):

        public ActionResult Index()
        {
            bindCombo();
            return View();
        }

        private void bindCombo()
        {
            List<SelectListItem> ObjItem = new List<SelectListItem>()
            {
                new SelectListItem {Text="Select",Value="Select",Selected=true },
                new SelectListItem {Text="ASP.NET",Value="ASP.NET" },
                new SelectListItem {Text="C#",Value="C#"},
                new SelectListItem {Text="MVC",Value="MVC"},
                new SelectListItem {Text="SQL",Value="SQL" },
            };
            ViewBag.ListItem = ObjItem;
        }

        [HttpPost]
        public ActionResult dropDown(string ListItem)
        {
            ViewBag.SelectedValue = ListItem;
            bindCombo();
            return View("index");
        }
  • Thank you so much, this is what stackoverflow.com is all about, you take my crappy code and you turn it into something that works and that I can copy/paste, you deserve a medal sir, I cant thank you enough. –  May 19 '16 at 05:16
0

Like Stephen Muecke said, you can't post the list.

Change this line in index.cshtml file

@Html.DropDownList("ListItem",(List<SelectListItem>)ViewBag.ListItem)

Change the dropDown method in controller

public ActionResult dropDown(int ListItem)
{
    List<SelectListItem> ObjItem = new List<SelectListItem>()
    {
        new SelectListItem {Text="Select",Value="0",Selected=true },
        new SelectListItem {Text="ASP.NET",Value="1" },
        new SelectListItem {Text="C#",Value="2"},
        new SelectListItem {Text="MVC",Value="3"},
        new SelectListItem {Text="SQL",Value="4" },
    };

    ViewBag.ListItem = ObjItem;
    ViewBag.SelectedValue = ListItem;

    return View("Index");
}
Ppp
  • 1,015
  • 9
  • 14
  • ok I made the changes, here's what I got: An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: There is no ViewData item of type 'IEnumerable' that has the key 'ListItem'. –  May 19 '16 at 05:10
0

The issue is that you cannot receive the SelectListItem when you click submit. Your action will receive the idx. This code will do what you want:

    List<SelectListItem> ObjItem = new List<SelectListItem>()
    {
  new SelectListItem {Text="Select",Value="0",Selected=true },
  new SelectListItem {Text="ASP.NET",Value="1" },
  new SelectListItem {Text="C#",Value="2"},
  new SelectListItem {Text="MVC",Value="3"},
  new SelectListItem {Text="SQL",Value="4" },
    };

    public ActionResult Index()
    {

        ViewBag.ListItem = ObjItem;

        return View("Index");
    }

    public ActionResult dropDown(string ListItem)
    {
        ViewBag.ListItem = ObjItem;
        ViewBag.SelectedValue = ObjItem.Where(i => i.Value == ListItem).FirstOrDefault().Text;


        return this.View("Index");
    }
Paulo Prestes
  • 484
  • 2
  • 8