【问题标题】:Return dropbox as a partialview based on selection of another dropbox根据对另一个 Dropbox 的选择将 Dropbox 作为局部视图返回
【发布时间】:2021-01-29 19:27:20
【问题描述】:

控制器代码:

        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();
        }

Dropbox 控制器代码:

        [HttpGet]
        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("CreatePopulateSCat") : View("Create");
        }

创建.cshtml:

            <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">Pasirinkite kategoriją</option>
            </select>
@Html.Partial("CreatePopulateSCat", Model)

ajax 代码:

<script type="text/javascript">
    function funct() {
        var catvalue = document.getElementById("SCat_Cat").value;
        var url = "CreatePopulateSCat"
        alert(catvalue);
        $.ajax(
            {
                type: "GET",
                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>

我已经尝试了很多不同的迭代,通常我被抛出一个错误,说找不到根元素,这个迭代实际上激活了控制器代码,但它没有返回“成功”或“错误” ajax 脚本中的代码。什么都没有发生。

非常感谢您的帮助。

【问题讨论】:

  • alert("Good");alert(error); 执行这些 js 中的任何一个?
  • 是的,它们是 Js 执行的。
  • 然后你从控制器获取数据。如果你在成功消息中写console.log(result),你会看到什么?
  • 哦,对不起,我误读了这个问题,不,他们不执行。对不起。

标签: .net ajax asp.net-mvc asp.net-core model-view-controller


【解决方案1】:

您正在从 CreatePopulateSCat(string CatID) 操作返回 IActionResult 类型对象。但在 UI 中,您正在尝试阅读 result.successIActionResult 没有 success 属性。所以你的js无论如何都不会工作。

如果您尝试使用 JSON 对象,那么为什么不从控制器返回 JSON 对象呢?渲染的视图 html 可以是 json 的属性。

return Json(new { success = true, result = "ViewResult" });

【讨论】:

  • 好的,我们取得了进展,我收到了成功警报,但现在保管箱没有被填充。 document.getElementById("SCat_SCat").value = result;这可能不会削减它,对吧?
  • 顺便谢谢你,对不起,如果我显得粗鲁,我很高兴终于解决了这个问题哈哈
  • 这与答案中提到的原因不同。 IActionResult 不能放在 dropdon 中。您需要将&lt;option value=""&gt;&lt;/option&gt; 元素添加到“SCat_SCat”对象。
猜你喜欢
  • 2015-06-08
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多