Основное веб-приложение Mvc
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
Что я уже пробовал:
<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>
Mohibur Rashid
улучшите этот вариант
данные: '{id: "' + $("#cmbEmpBranchCode").val() + '" }',
к
данные: {id: $("#cmbEmpBranchCode"),
Как вы возвращаете данные?
Это JSON?
Это просто обычная струна?
Понять это.
Member 13140365
обычный текст