我想在从下拉选择列表中选择 Id 时显示与特定卡相关的余额,并在 [在此处输入图像描述][1] 表单上的输入中显示余额值。
但是我得到的只是运行程序时的 JSON 结果。我的表格没有出现
控制器中的测试操作:
public IActionResult Test()
{
var data = from als in ctx.TbIstanbulCards select new { als.IstanbulCardId, als.Balance };
return Json(data.ToList());
}
该Index
图(试验)
@model VmIstanbul
@section Scripts{
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
var options = {};
options.url = "/Customer?handler=SelectAll";
options.type = "GET";
options.dataType = "json";
options.success = function (data) {
data.forEach(function (element) {
$("#customerid").append("<option>
"
+ element.IstanbulCardId + "
</option > ");
});
};
options.error = function () {
$("#msg").html("Error while
making Ajax call!");
};
$.ajax(options);
});
$("#customerid").change(function () {
var options = {};
options.url = "/Customer/" +
$("#customerid").val() + "?handler=SelectByID";
options.type = "GET";
options.dataType = "json";
options.success = function (data) {
$("#companyname").val(data.Balance);
//$("#contactname").val(data.contactName);
//$("#country").val(data.country);
};
options.error = function () {
$("#msg").html("Error while making Ajax call!");
};
$.ajax(options);
});
</script>
}
<form id="myForm" asp-controller="Customer" asp-action="Test">
<label for="departmentsDropdown"><b>Card</b></label>
<select class="form-control" onchange="getValue()" id="departmentsDropdown" name="departmentsDropdown"></select><br />
<input type="text" value="" id="show" />
</form>
结果看起来像这样
从您的代码中不清楚,因为 ajax url 是剃刀页面路由而不是 mvc。你启用它们吗?
此外,从您之前的帖子中,我看到视图名称是Test.cshtml
,而Test
操作返回 Json,因此当您访问 时/Custom/Test
,您只会获得 Json 结果而不是视图。
如果使用 Test 动作来显示 html 视图,我认为它应该返回 View:
public IActionResult Test()
{
return View();
}
更新:
有一个动作(测试)返回视图(Test.cshtml)。只是reutn View()
没有任何数据,因为我没有看到您在视图中使用模型(VmIstanbul)值的任何地方。
public IActionResult Test()
{
return View();
}
有另一个动作将数据返回给 ajax,给它一个名字,除了Test
. 例如:
public IActionResult TestSelect()
{
var data = from als in ctx.TbIstanbulCards select new { als.IstanbulCardId, als.Balance };
return Json(data.ToList());
}
并将ajax url更改为 TestSelect
@section Scripts{
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/Customer/TestSelect",
success: function (data) {
console.log(data);
var s = '<option value="-1">Please Select a Card id</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].balance + '">' + data[i].istanbulCardId + '</option>';
}
$("#departmentsDropdown").html(s);
}
})
})
function getValue() {
var myVal = $("#departmentsDropdown").val();
$("#show").val(myVal);
}
</script>
}
结果:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句