如何解决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 举报,一经查实,本站将立刻删除。