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
Release: 2025-01-14 08:03:43
Original
565 people have browsed it

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>
Copy after login
<code class="language-csharp">[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV, FormCollection form)
{           
  string strDDLValue = form["ddlVendor"].ToString();
  return View(MV);
}</code>
Copy after login

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>
Copy after login

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

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

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>
Copy after login

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>
Copy after login
<code class="language-html">@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")
@Html.HiddenFor(m=>m.SelectedvendorText)
...</code>
Copy after login
<code class="language-javascript">$("#SelectedVendor").on("change", function() { 
    $(this).text(); 
});</code>
Copy after login

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!

source:php.cn
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