How to pass value of selected dropdown list from View to Controller in MVC

mvc dropdownlist selected value viewbag
how to pass dropdown selected value from view to controller using jquery
how to pass dropdown selected value from view to controller in mvc
how to pass dropdown selected value to controller in mvc using jquery
how to get selected value from dropdownlist in mvc 5
how to get dropdown selected text in mvc controller
how to pass dropdown selected value to controller in mvc on button click
how to get selected value from dropdownlist in mvc 4 razor

Hi i need some help on MVC as new in this area. Here is my code for Dropdown list and I just want to get the selected values by user in a controller so that I can pass that value to a table in db. Here is the code from .cshtml

@Html.DropDownList("LogOffTime", new List<SelectListItem>
{
    new SelectListItem{ Text="One", Value = "30000" },
    new SelectListItem{ Text="Two", Value = "60000" },
    new SelectListItem{ Text="Three", Value = "180000" }
}, "Select Time") 

Controller:

 public ActionResult Index()
 {How to get the value from drop down list}

I just need to know how to access the value i.e. the 30000/60000 etc. in controller. Also need to check that no value will be passed if the user does not select anything. Also correct me pls. if anything wrong in my code. Thanks!

Create a model

public class dropdown {
public int value {get;set;}
}

You need to add using around your dropdown list like this, Add a submit button which will do a post to the controller-

    @using(Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
         {
         @Html.DropDownList(m=>m.value,"LogOffTime", new List<SelectListItem>
            {
                new SelectListItem{ Text="One", Value = "30000" },
                new SelectListItem{ Text="Two", Value = "60000" },
                new SelectListItem{ Text="Three", Value = "180000" }
            }, "Select Time")
 <input type="submit" value="Submit" />
 }

In your controller

public ActionResult AddItem(dropdown value) // you will get the value here
{
  // do something 

}

How to get DropDownList SelectedValue in Controller in MVC , What's happening in your code now, is you're passing the first string argument of "ddlVendor" to Html. DropDownList , and that's telling the MVC framework to create a <select> element with a name of "ddlVendor." When the user submits the form client-side, then, it will contain a value to that key. What's happening in your code now, is you're passing the first string argument of "ddlVendor" to Html.DropDownList, and that's telling the MVC framework to create a <select> element with a name of "ddlVendor." When the user submits the form client-side, then, it will contain a value to that key.

First of all you need a model.. Lets say you have Id column in your model..

 public ActionResult Index()
        {         
            ViewBag.MyList= new SelectList(db.MyTable.ToList(), "Id", "Name");    
            return View();
        }

In Razor

@Html.DropDownListFor(m => m.Id, ViewBag.MyList as SelectList, new { @class = "form-control" })

POST Controller Method

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(MyModel model)
{
  int getmyid=model.Id;
}

ASP.Net MVC: Pass (Send) DropDownList Selected Text and , Here Mudassar Ahmed Khan has explained with an example, how to pass (send) DropDownList Selected Text and Selected Value to Controller from View using� As we know that whenever we want to pass dropdown text from view to controller using the model object, the value of dropdown is transferred but in some cases, we required text of the drop-down. in this article I discuss that how we can perform this. Step1: Create New ASP.NET MVC empty project.

I have this example from what I am working right know. I hope it can help

View or DropDownList:

@using(Html.BeginForm("getSelectedValue", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.DropDownList("LogOffTime", new List<SelectListItem>
    {
       new SelectListItem{ Text="One", Value = "30000" },
       new SelectListItem{ Text="Two", Value = "60000" },
       new SelectListItem{ Text="Three", Value = "180000" }
    }, "Select Time")

    <input type="submit" value="Submit" />
}

HomeController:

[HttpPost]
public ActionResult getSelectedValue()
{
    var selectedValue = Request.Form["LogOffTime"].ToString(); //this will get selected value
}

ASP.Net MVC: Pass (Send) DropDownList Selected Value to , The values posted from the Form inside the View are received through this parameter. public class HomeController� Both will hit the controller method and pass the value of the selected option. The only difference is how its displayed in the browsers address bar when you make the redirect. – user3559349 Aug 2 '17 at 5:05

Its very simple you just need to understand only one thing ,For dropdownlist control, view form will post only the selected value along with name of dropdownlist control as a key-value pair to the controller method. So you can simply write like below to get value

View Code:

@Html.DropDownList("LogOffTime", new List<SelectListItem>
{
    new SelectListItem{ Text="One", Value = "30000" },
    new SelectListItem{ Text="Two", Value = "60000" },
    new SelectListItem{ Text="Three", Value = "180000" }
}, "Select Time") 

Controller Code:

 public ActionResult Index( string LogOffTime)  //LogOffTime is name of dropdownlist controll
{

           //some  logic
}

Note: You should put the ddcontrol inside your form tag, and post the data using the submit or you can also do the same thing with change event of jquery.

Hope the above information was helpful

Thanks

Karthik

How to pass dropdownlist selected value to controller., Hello..I am totally new to asp.net MVC,I want to set dropdownlist selected value to controller.I have Index and create view ,In create view I have� Hi i need some help on MVC as new in this area. Here is my code for Dropdown list and I just want to get the selected values by user in a controller so that I can pass that value to a table in db. Here is the code from .cshtml

SelectLists are one of the more challenging concepts for people (me) to grasp. Part of the confusion is that when we set the "Text", we intuitively think that "Value" is an number (int or "Id"). It's not. "Value" is passed from the view to the controller as a string. Then, when the form is submitted to the controller, you'll want to parse the "Value" and convert the value to an int. See below:

Your viewmodel should have a property of type 'string' that will hold the selected value.

ViewModel

public class dropdown {
public string selectedvalue {get;set;}
}

View

 @using(Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
         {
         @Html.DropDownListFor(m=>m.selectedvalue, new List<SelectListItem>
            {
                new SelectListItem{ Text="One", Value = "30000" },
                new SelectListItem{ Text="Two", Value = "60000" },
                new SelectListItem{ Text="Three", Value = "180000" }
            }, "Select Time")
 <input type="submit" value="Submit" />
 }

Controller

public ActionResult Index(string selectedValue)  
{
  int value = 0;
  bool valueIsInt = Int32.TryParse(selectedValue, out value); //this returns true if selectedValue can be converted to int and outputs the int as "value"

 if (valueIsInt)
{

selectedvalue = value        // if value is an int, set it to selectedvalue

}

//otherwise do something else

}

I know this is a little late, but hopefully this can help someone struggling with selectlists as I was.

how to send a dropdown value to controller in mvc5, if you bind model to view you will get the selected value directly in controller ( without binding the model also we can achieve the selected value� MVC get selected value and text from drop down list in controller More info with ready to copy/paste code http://howtodomssqlcsharpexcelaccess.blogspot.ca/20

Pass Dropdownlist Selected Value In MVC(From View To Controller , As we know that whenever we want to pass dropdown text from view to controller using the model object, the value of dropdown is transferred� The first parameter is the Name of the DropDownList, the same will also be used to fetch the selected value of the DropDownList inside Controller when the Form is submitted. The second parameter is the Model class for populating the DropDownList i.e. its source of data.

How to get DropDownList selected Text in MVC controller, I want to show you how to build a simple form with a drop down list that's I'll show you how to display that list on a form, how to get user's selection in the controller, can pass the data into the view: ViewBag, ViewData or TempData? can test rendering of selected dropdown list value on the postback. STEP BY STEP CLIENT SIDE GETTING SELECTED VALUE OF DROPDOWNLIST. To receive the selected value of dropdown list in client side we have to call ONCHANGE event of javascript. In this we will achieve the task only by changing VIEW FILE (MemberEntry.CSHTML) ONCHANGE event we will call a function.

Using simple Drop Down Lists in ASP.NET MVC, mvc get dropdownlistfor selected value, text using jQurey into textbox from database #mvc Duration: 13:08 Posted: Aug 8, 2018

Comments
  • Read a tutorial about asp.net mvc.
  • @ataravati that comment does not solve his issue, neither does it give him an insight into how he can resort to resolving it.
  • @UmarAftab When I made that comment the question didn’t have any code.
  • thanks, but I am not sure as getting error implementing on lambda expression it says "can not convert lambda expression to string.."
  • Note that Index in the BeginForm line should actually be the name of the Controller function, AddItem, instead.
  • Where suppose to set the values.
  • @Zahir I edited it just like your examples. check it
  • Note that the dropdown list MUST go inside a @using(Html.BeginForm("getSelectedValue", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { ... } block that the button is also inside of, and actionResult should be ActionResult. Submitted edits.