XML分析错误:将.ajax与值传递到同一页面时,找不到根元素 Asp.net 3.1 MVC

问题描述

我正在尝试发送一个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</option>
</select>

ajax脚本

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

控制器代码

        public async Task<IActionResult> Create(string CatID)
        {

            ViewBag.Cat_ID = new SelectList(_context.Category,"Cat_ID","Cat_Name");
            if (CatID == null)
            {    
                ViewBag.SCat_ID = new SelectList("");
            }
            else
            {
                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");
            }
            var specialists = await userManager.GetUsersInRoleAsync("Specialist");
            ViewBag.Specialist_ID = new SelectList(specialists,"Id","UserName");
            return Request.IsAjaxRequest() ? (IActionResult)PartialView() : View();
        }

我尝试过的替代控制器代码分别处理此问题

    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_Name");

        return Request.IsAjaxRequest() ? (IActionResult)PartialView("Create") : View();
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)