[英]Return dropbox as a partialview based on selection of another dropbox
Controller code:控制器代码:
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 controller code: 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");
}
Create.cshtml:创建.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 code:阿贾克斯代码:
<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>
I've tried many different iterations of this, and usually I was thrown back an error saying Root element not found, this iteration actually activates the controller code, but it doesn't return either the "success" or "error" code in ajax script.我已经尝试了很多不同的迭代,通常我被抛出一个错误,说找不到根元素,这个迭代实际上激活了控制器代码,但它不会在 ajax 中返回“成功”或“错误”代码脚本。 Just nothing happens.只是什么都没有发生。
I would really appreciate the help.我真的很感激你的帮助。
You are returning IActionResult
type object from the CreatePopulateSCat(string CatID)
Action.您正在从CreatePopulateSCat(string CatID)
操作返回IActionResult
类型对象。 but in the UI you are trying to read result.success
.但在用户界面中,您正在尝试读取result.success
。 IActionResult
has got no success
property. IActionResult
没有success
属性。 So your js will not work anyways.所以你的js无论如何都不会工作。
If you are trying to work with JSON object then why not return a JSON object from the controller instead?如果您尝试使用 JSON 对象,那么为什么不从控制器返回 JSON 对象呢? The rendered view html can be a property of the json.呈现的视图 html 可以是 json 的一个属性。
return Json(new { success = true, result = "ViewResult" });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.