Home >Backend Development >C++ >How to Retrieve a DropDownList's Selected Value in an ASP.NET MVC Controller?

How to Retrieve a DropDownList's Selected Value in an ASP.NET MVC Controller?

Barbara Streisand
Barbara StreisandOriginal
2025-01-14 08:03:43563browse

How to Retrieve a DropDownList's Selected Value in an ASP.NET MVC Controller?

Get the selected value of DropDownList in ASP.NET MVC controller

In ASP.NET MVC development, it is often necessary to obtain the selected value of the DropDownList in the controller to perform data processing and verification based on the user's selection. This article introduces two methods:

Method 1: Through Request or FormCollection

This method gets the selected value directly from the HTTP request. Depending on the name of the dropdown (ddlVendor), use one of the following code snippets:

<code class="language-csharp">string strDDLValue = Request.Form["ddlVendor"].ToString();</code>
<code class="language-csharp">[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV, FormCollection form)
{           
  string strDDLValue = form["ddlVendor"].ToString();
  return View(MV);
}</code>

Method 2: Through model binding

To use model binding, you need to add an attribute to the model to store the selected value:

<code class="language-csharp">public class MobileViewModel 
{          
    ...
    public string SelectedVendor { get; set; }
}</code>

In the view, update the DropDownList to use this property:

<code class="language-html">@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")</code>

In the HttpPost operation, the selected value will be automatically bound to the model and accessed in the controller:

<code class="language-csharp">[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{           
   string SelectedValue = MV.SelectedVendor;
   return View(MV);
}</code>

Update: Get selected item text

If you need to get the text of the selected item instead of its value, you can add a hidden field and use JavaScript to update its value based on the selection of the drop-down list:

<code class="language-csharp">public class MobileViewModel 
{          
    ...
    public string SelectedvendorText { get; set; }
}
...</code>
<code class="language-html">@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")
@Html.HiddenFor(m=>m.SelectedvendorText)
...</code>
<code class="language-javascript">$("#SelectedVendor").on("change", function() { 
    $(this).text(); 
});</code>

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

Statement:
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