微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.ajax函数结果似乎未从控制器返回

如何解决.ajax函数结果似乎未从控制器返回

我有一个Ajax函数,它会在我的下拉列表更改时触发:

<select id="SCat_Cat" name="SCat_Cat" asp-for="Cat_Id" asp-items="(SelectList)ViewBag.Cat_ID" class="form-control" onchange="funct()">
      <option value="0">Select Category</option>
</select>

应对此进行更新:

<select id="SCat_SCat" name="SCat_SCat" asp-for="SCat_ID" asp-items="(SelectList)ViewBag.Scat_ID" class="form-control">
       <option value="0">Select SubCategory</option>
</select>

.ajax代码

<script type="text/javascript">
    function funct() {
        var catvalue = document.getElementById("SCat_Cat").value;
        var url = "CreatePopulateScat"
        alert(catvalue);
        $.ajax(
            {
                type: "POST",url: url,data: { CatID: catvalue },success: function (result) {
                    if (result.success) {
                        alert("Good");
                        document.getElementById("SCat_SCat").innerHTML = result;                     
                    }
                },error: function (req,status,error) {
                    alert("Error");
                }
            });
        return false;
    }
</script>

以及控制器代码

        public async Task<IActionResult> Create()
        {
            ViewBag.Cat_ID = new SelectList(_context.Category,"Cat_ID","Cat_Name");
            var specialists = await userManager.GetUsersInRoleAsync("Specialist");
            ViewBag.Specialist_ID = new SelectList(specialists,"Id","UserName");
            return Request.IsAjaxRequest() ? (IActionResult)PartialView() : View();
        }

        [HttpPost]
        public IActionResult CreatePopulateSCat(string CatID)
        {
            List<SubCategory> AllSubCategories = _context.SubCategory.ToList();
            List<SubCategory> SelectedSubCategories = AllSubCategories.FindAll(a => a.SCat_Cat.Contains(CatID));
            ViewBag.SCat_ID = new SelectList(SelectedSubCategories,"SCat_ID","SCat_Name");
            return Request.IsAjaxRequest() ? (IActionResult)PartialView("Create") : View("");
        }

使用断点,我可以知道ajax脚本向控制器发送了正确的数据,并且收集了列表,但是它不返回任何内容,甚至没有经过success代码显示alert("Good");alert("Error");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。