【发布时间】:2020-08-08 00:45:08
【问题描述】:
我正在寻求有关此主题的帮助。
我的视图中有 2 个文本框。 当 textbox1 发生更改时,我想将一个值(在我的控制器中生成)应用于 textbox2。 例如,我在 textbox1 中输入用户名,textbox2 是从控制器获取的电子邮件地址(我们可以想象控制器检查数据库或活动目录)。
我下面的代码似乎不起作用:
在我的视图文件中,我添加了一个脚本部分:
@section scripts{
<script type="text/javascript" language="javascript">
function ShowPersonalPanel(myChkboxPersonalLine) {
if (myChkboxPersonalLine.checked == false) {
$("#1-w").hide("fast");
}
else {
$("#1-w").show("fast");
}
}
</script>
<script language="javascript">
$("#1-w").hide("fast");
$('#myOtherGID').change(function () {
var myOtherGID = this.value;
$.getJSON("/PhoneController/GetEmailByGID",
{
strGID: myOtherGID
},
function (data) {
$('myOtherEmail').val(data);
});
});
</script>
}
以及带有文本框的视图中的代码:
<label for="myOtherGIDlbl">GID:</label>
<label class="text" id="myOtherGIDlbl">
@Html.TextBoxFor(model => model.myOtherGID)
</label>
<label for="myOtherEmaillbl">Email:</label>
<label class="text" id="myOtherEmaillbl">
@Html.TextBoxFor(model => model.myOtherEmail)
</label>
在我的控制器(名为 PhoneController)上,我添加了一个动作:
public JsonResult GetEmailByGID(string strGID)
{
string strEmailAddress = "this is my email";
return Json(strEmailAddress, JsonRequestBehavior.AllowGet);
}
当我使用 _layout.cshtml 和 viewFile.cshtml 时,我在布局(在头部)中添加了以下行:
<script type="text/javascript" src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
我错过了什么?
问候
【问题讨论】:
标签: javascript html asp.net ajax asp.net-mvc