0

I have a simple form in Asp.Net MVC form and I had a dropdown list with values pulled from a view data. How can I get the value of selected value from dropdown to the controller object to use inside httppost method?

@Html.LabelFor(model => model.ItemType)
@Html.DropDownList("ItemTypes", new SelectList((IEnumerable)ViewData["itemTypes"], "Value", "Text"))

<input type="submit" value="Request Item" />


ViewData["itemTypes"] = GetItemTypes();
    [HttpPost]
    public ActionResult NewVc(ItemInfoViewModel itemInfoObject)
    {
    //Item not populated to itemInfoObject
    }

Pull Item types from

 public static List<ListItem> GetItemTypes()
        {
            var itemTypes = new List<ListItem>
            {
                new ListItem {Text = "Select", Value = ""},
                new ListItem {Text = "Item1", Value = "Item1"},
                new ListItem {Text = "Item2", Value = "Item2"},
                new ListItem {Text = "Item3", Value = "Item3"}
            };
            return itemTypes;
        }
4
  • 1
    Does your ItemInfoViewModel contain a property named ItemTypes (but in any case do not use the same name for the property your bind to and the name of the SelectList) Commented Dec 15, 2016 at 0:38
  • My viewmodel has the property Commented Dec 15, 2016 at 0:39
  • 1
    Then assuming its string ItemTypes it will be bound with the selected option value. But this is awful practice and you should be using a view model with properties string SelectedItemType` and IEnumerable<SelectListItem> ItemTypeList and using the strongly typed @Html.DropDowmListFor(m => m.SelectedItemType, Model.ItemTypeList) Commented Dec 15, 2016 at 0:42
  • Based on your Html.LabelFor(model => model.ItemType) you model contains a property named ItemType (which is not the same as ItemTypes - plural) Commented Dec 15, 2016 at 0:54

1 Answer 1

2

If not exist, add a new property to store the selected option

public class ItemInfoViewModel 
{
  public string SelectedItemType { set;get;}
  // your existing properties
}

Now in your GET action method, you can use the Html.DropDownListFor helper inside the form.

@model ItemInfoViewModel 
@using(Html.BeginForm("NewVc","PutYourControllerNameHere"))
{
   @Html.DropDownListFor(s=>s.SelectedItemType, 
                                           ViewData["itemTypes"] as List<SelectListItem>)
   <input type="submit" />

}

Replace PutYourControllerNameHere with your actual controller name where you have the NewVc action method. You do not need the Controller Suffix.

The above code basically generates the HTML tag for a SELECT element with name "SelectItemType" inside a form tag. When you submit the form, model binder will be able to map the posted form data to the property of your ItemInfoViewModel object, which is the parameter of your HttpPost action method.

Also you should change the return type of your GetItemTypes method to a collection of SelectListItem since our view code is trying to cast it directly a List<SelectListItem>

public static List<SelectListItem> GetItemTypes()
{
        var itemTypes = new List<SelectListItem>
        {
            new SelectListItem{Text = "Select", Value = ""},
            new SelectListItem{Text = "Item1", Value = "Item1"},
            new SelectListItem{Text = "Item2", Value = "Item2"},
            new SelectListItem{Text = "Item3", Value = "Item3"}
        };
        return itemTypes;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

This throws error Compiler Error Message: CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type.
Sorry ! I did not pay attention to your GetItemTypes method. See the edit in answer..
awesome worked like a charm. Changes did for community reference: 1. SelectListItem 2. DropdownListFor

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.