Home > Backend Development > C++ > How to Get the Selected Value from a DropDownList in an MVC Controller?

How to Get the Selected Value from a DropDownList in an MVC Controller?

Susan Sarandon
Release: 2025-01-14 07:48:46
Original
769 people have browsed it

How to Get the Selected Value from a DropDownList in an MVC Controller?

Get SelectedValue of dropdown list in MVC controller

In MVC applications, assigning values ​​to drop-down lists from the database is a very common scenario. However, when you submit the form, you need to access the selected value in the controller. This article provides two methods to achieve this:

Method 1: Use Request or FormCollection

The first method reads the selected value directly from the request. Using Request.Form you can specify the key of the dropdown (ddlVendor in this case) to retrieve the posted value:

string strDDLValue = Request.Form["ddlVendor"].ToString();
Copy after login

Alternatively, you can use FormCollection:

[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV, FormCollection form)
{
  string strDDLValue = form["ddlVendor"].ToString();
  return View(MV);
}
Copy after login

Method 2: Use model

For the model binding method, add a property to your model:

public class MobileViewModel 
{          
    public List<tbinsertmobile> MobileList;
    public SelectList Vendor { get; set; }
    public string SelectedVendor {get;set;}
}
Copy after login

In your view, bind the dropdown list to the model using @Html.DropDownListFor and specify the SelectedVendor property:

@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")
Copy after login

Finally, in your controller, the selected value can be accessed via the SelectedVendor property:

[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{           
   string SelectedValue = MV.SelectedVendor;
   return View(MV);
}
Copy after login

Update: Post selected text

To capture both the selected value and its corresponding text, add a hidden field to your view:

public class MobileViewModel 
{          
    public List<tbinsertmobile> MobileList;
    public SelectList Vendor { get; set; }
    public string SelectVendor {get;set;}
    public string SelectedvendorText { get; set; }
}
Copy after login

In your view, use JavaScript to update the selected item text in the hidden field:

$(function(){
$("#SelectedVendor").on("change", function() {
   $("#SelectedvendorText").val($(this).text());
 });
});

@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")
@Html.HiddenFor(m=>m.SelectedvendorText)
Copy after login

The above is the detailed content of How to Get the Selected Value from a DropDownList in an MVC Controller?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template