ASP.NET核心剃刀页面中的Ajax请求 - 什么都不做 [英] Ajax request in ASP.NET core razor pages - doing nothing
问题描述
我的目标是每次对下拉列表进行更改时动态更新两个字段。在我的示例中,下拉列表是员工列表,我需要每次更改时更新组/子组字段。
使用这篇文章和其他人,我试图称之为C#方法我是创建了哪个运行2个linq查询并将结果设置为字段。我已经证实这种方法效果很好。它位于Edit.cshtml.cs中:
My goal is to dynamically update two fields every time there's a change made to a dropdown list. In my example, the dropdown is a list of employees, and I need the group/subgroup fields to update every time it changes.
Using this article and others, I'm attempting to call the C# method I've created which runs 2 linq queries and sets the result to the fields. I've verified that this method works just fine. This is located in "Edit.cshtml.cs":
// Displays chosen employee's group and subgroup
public ActionResult OnPostSearchCurrGroup()
{
var subgroupQuery = (from c in _context.ppcc_matrix
from e in _context.employees
from s in _context.subGroups
where c.employeesID == e.Id
where e.subGroupsID == s.Id
select s.subgrp_nm).FirstOrDefault();
subGroups.subgrp_nm = subgroupQuery;
var groupQuery = (from c in _context.ppcc_matrix
from e in _context.employees
from s in _context.subGroups
from g in _context.groups
where c.employeesID == e.Id
where e.subGroupsID == s.Id
where s.groupsID == g.Id
select g.grp_nm).FirstOrDefault();
groups.grp_nm = groupQuery;
return Page();
}
在我的Edit.cshtml文件中,我有3个字段:
In my "Edit.cshtml" file, I have my 3 fields:
<div class="form-group">
<label asp-for="ppcc_matrix.employeesID" class="control-label"></label>
<select asp-for="ppcc_matrix.employeesID" class="form-control" id="employee" asp-items="ViewBag.employeesID">
<option disabled selected value="0" style="display:none">--select--</option>
</select>
</div>
<div class="form-group">
<label asp-for="groups.grp_nm" id="grptxt">Group</label>
<input asp-for="groups.grp_nm" id="grpnm" class="form-control" disabled />
</div>
<div class="form-group">
<label asp-for="subGroups.subgrp_nm" id="subgrptxt">SubGroup</label>
<input asp-for="subGroups.subgrp_nm" id="subgrpnm" class="form-control" disabled />
</div>
然后我的javascript:
and then my javascript:
<script language="javascript" type="text/javascript">
var employee = document.getElementById('employee');
employee.addEventListener('change', UpdateGroup);
function UpdateGroup() {
$.ajax({
type: 'post',
url: '/edit?handler=searchcurrgroup',
contenttype: "application/json; charset=utf-8",
datatype: 'json'
})
}
</script>
问题:当我更改员工字段时,它什么都不做。我的处理程序命名正确,它是一个onpost方法..
如果我将它设置为按钮上的asp-page-handler,它会起作用,但当然使用ajax调用会更有意义自动显示。
我尝试过:
多篇文章这样的和上面的代码。
推荐答案
.ajax({
type:'post',
url:'/ edit?handler = searchcurrgroup',
contenttype:application / json; charset = utf-8,
datatype:'json'
})
}
< / script>
.ajax({ type: 'post', url: '/edit?handler=searchcurrgroup', contenttype: "application/json; charset=utf-8", datatype: 'json' }) } </script>
问题:当我更改员工字段时,它什么都不做。我的处理程序命名正确,它是一个onpost方法..
如果我将它设置为按钮上的asp-page-handler,它会起作用,但当然使用ajax调用会更有意义自动显示。
我尝试过:
多篇文章这样的和上面的代码。
样本之间的差异就在于你返回的内容。
你的控制器动作的最后一行是return Page();
,它将返回一个没有数据传递给它的普通页面结果。
最后一个样本行都是返回新的JsonResult(lstString);
,它将返回一个填充了 lstString 模型的JsonResult作为数据源。 />
所以你的任务是弄清楚如何获得结果将您的2个查询转换为一个模型并将其传递到相应的 return 操作
The difference between the samples is all in what you are returning.
The last line in your controller action isreturn Page();
, which will return a plain Page Result with no data being passed into it.
The last line of the samples are allreturn new JsonResult(lstString);
which will return a JsonResult populated with the lstString model as a data source.
So what your task is going to be is to figure out how to get the results of your 2 queries into one model and pass that into the appropriate return action
OnPostSearchCurrGroup()方法需要返回Json对象,因此它应该是JsonResult类型actionresult并且应该返回新的JsonResult(lstString)而不是返回Page()。
OnPostSearchCurrGroup() method needs to return Json object,hence it should be of type JsonResult instead actionresult and should return new JsonResult(lstString) instead return Page().
这篇关于ASP.NET核心剃刀页面中的Ajax请求 - 什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!