Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one Dropdown List and one TextBox control in my View. The Dropdown list contains branch codes and when i select the branch code i want to display its associated branch name in the TextBox control. I created the following action Method:
 
public string RerieveBranchName(string id)
{
var branchName = from e in dbContext.CompanyBranches
                               where e.BranchCode == id
                               select e;
ViewBag.SelectedBranchID = branchName.ToString();
return "0"; 
}
 
 Now my problem is how to pass the branch id to the action method when i select it from the dropdown list and after passing the branch id how to return the result and display in the TextBox.
 
Please help me. Thanks in advance
 
Please write your code in Razor syntax. Thanks again


What I have tried:

<div class="col-md-3">
            <div class="form-group">
                <label asp-for="EmpBranchCode" class="control-label label-size">Emp Branch Code</label>
                <select asp-for="EmpBranchCode" id="cmbEmpBranchCode" class="form-control textbox" asp-items="ViewBag.EmpBranchCode" onchange="RetrieveBranchName(this);"></select>
      </div>
        </div>
<div class="col-md-3">
            <div class="form-group">
                <label class="control-label label-size"> Branch Name</label>
                <input id="EmpBranchName" class="form-control textbox" value="@ViewBag.SelectedBranchID"/> 
            </div>
        </div>



<script type="text/javascript">
        function RetrieveBranchName(selectedBranch) {
            var selectedBranchID = selectedBranch.value;
            $.ajax({
                type: "POST",
                url: "/EmployeeGeneralDetails/RerieveBranchName",
                data: '{id: "' + $("#cmbEmpBranchCode").val() + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                 
                //Here i am confuse to take the result of the Action Method and display it into the TextBox   
                 
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });
                }
</script>
Posted
Comments
Mohibur Rashid 26-Sep-18 23:51pm    
improve this one
data: '{id: "' + $("#cmbEmpBranchCode").val() + '" }',
to
data: {id: $("#cmbEmpBranchCode"),


How are you returning the data?
Is it JSON?
Is it Just plain string?

Figure it out.
Member 13140365 27-Sep-18 17:30pm    
Plain Text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900