Mohammad Jalal Ahmadzai Mohammad Jalal Ahmadzai - 3 years ago 193
ASP.NET (C#) Question

how to access text of a dropdownlist not it's value when submiting the form to controller in asp.net mvc5

I need to access text and value both of them from dropdownlist in in controller!
enter image description here
in this pic which you can see I need to access Import Duty text in controller when I submit the form!!!

public ActionResult Index(){ string taxtype=Request["TaxType"];//this shows the value I need this to access it's text like
string txt=Request["TaxType"].Text; }

Answer Source

Here you can do like, you need to fire javascript function on your drop down selection change and set that text to hidden text box and pass that value to your action method like below

This will be your html code.

 <select id="TaxType" name="TaxType" onchange="SetTaxTextValue();">
                <option value="op1">Option1</option>
                <option value="op2">Option2</option>
                <option value="op3">Option3</option>
                <option value="op4">Option4</option>
            </select>
            <input type="text" style="visibility:hidden" id="txtTax" name="txtTax" />

Below is javascript function

function SetTaxTextValue() {
        var taxText = $("#TaxType option:selected").text();;
        $("#txtTax").val(taxText);
    }

and your controller action will be below

[HttpPost]
        public ActionResult Index(string TaxType, string txtTax)
        {
            string finalVal = TaxType + txtTax;
            return View();
        }

Hope this will help.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download