Mvc核心Web应用程序 [英] Mvc core web application

查看:87
本文介绍了Mvc核心Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>

推荐答案

.ajax({
type:POST ,
url:/ EmployeeGeneralDetails / RerieveBranchName,
data:'{id:'+
.ajax({ type: "POST", url: "/EmployeeGeneralDetails/RerieveBranchName", data: '{id: "' +


(#cmbEmpBranchCode)。val()+ '}',
contentType:application / json; charset = utf-8,
dataType:json,
success:function(data){

//这里我很困惑地采取行动方法的结果并将其显示在TextBox

},
错误:函数(响应){
alert(响应) .responseText);
}
});
}
< / script>
("#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>


这篇关于Mvc核心Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆