1

I have dropdownlist and want to pass value in Controller. View

@using (Html.BeginForm())
{  
    @Html.DropDownList("dropOrg", ViewBag.dropOrg as SelectList)
    <input type="submit" value="save" />
}

Controller

foreach (int tmp in org)
{
   string s = tmp + " - " + orgNames[tmp];
   SelectListItem item1 = new SelectListItem() { Text = s, Value = tmp.ToString() };
   items.Add(item1);
}
ViewBag.dropOrg = items;

What should i do?

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • How about taking a [tutorial](https://www.pluralsight.com/guides/asp-net-mvc-populating-dropdown-lists-in-razor-views-using-the-mvvm-design-pattern-entity-framework-and-ajax)? – Renatas M. Nov 26 '18 at 11:18
  • I would suggest using a DropDownListFor for this. If set on this, I would go with calling a javasript function and using jquery to get the value. Passing it to the controller using an ajax. – JamesS Nov 26 '18 at 11:20
  • Is there any easyer way to do it? – Giorgi Asanidze Nov 26 '18 at 11:39
  • Possible duplicate of [How to get DropDownList SelectedValue in Controller in MVC](https://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc) – Tetsuya Yamamoto Nov 27 '18 at 01:29

1 Answers1

1

It will be better if you create ViewModel for your View:

public class SampleViewModel
{
    public string DropDownListValue { get; set; }
}

then in your controller's get method:

public ActionResult SomeAction()
{
    var org = GetOrg(); //your org
    var orgNames = GetOrgNames(); //your orgNames

    // . . .

    ViewBag.DropDownListValue = new SelectList(org.Select(s => 
    new SampleViewModel 
    {  
        DropDownListValue = $"{s} - {orgNames[s]}"
    }, "DropDownListValue", "DropDownListValue");
    return View(new SampleViewModel())
}

your SomeAction View:

@model YourAppNamespace.SampleViewModel
<h1>Hello Stranger</h1>

@using (Html.BeginForm())
{
    @Html.DropDownList("DropDownListValue")
    <input type="submit" value="Submit"/>
}

Note that:

The DropDownList helper used to create an HTML select list requires a IEnumerable<SelectListItem>, either explicitly or implicitly. That is, you can pass the IEnumerable<SelectListItem> explicitly to the DropDownList helper or you can add the IEnumerable<SelectListItem> to the ViewBag using the same name for the SelectListItem as the model property.

We have used here implicit passing, that is we have used same name for SelectListItem and ViewBag (which is DropDownListValue).

Then when you hit Submit, you need HttpPost method for SomeAction:

[HttpPost]
public ActionResult SomeAction(SampleViewModel model)
{
    var org = GetOrg(); //your org
    var orgNames = GetOrgNames(); //your orgNames

    //. . . Validation etc..

    ViewBag.DropDownListValue = new SelectList(org.Select(s => 
    new SampleViewModel 
    {  
        DropDownListValue = $"{s} - {orgNames[s]}"
    }, "DropDownListValue", "DropDownListValue", model.DropDownListValue);


    var doSomething = model.DropDownListValue; //Your selected value from DropDownList
    return View(model)
}

References: DotNetFiddle Example, Using the DropDownList Helper with ASP.NET MVC

SᴇM
  • 7,024
  • 3
  • 24
  • 41