【发布时间】:2021-08-22 00:40:43
【问题描述】:
我想从局部视图的下拉列表中获取用户输入,并将其传递给索引页面中的 js 函数,以根据 QuestionType 属性的值过滤掉结果。 因此,视图模型如下所示:
public class TestQuestionModel
{
public Guid TestId { get; set; }
public Guid QuestionId { get; set; }
public string Id { get; set; }
public string QuestionType { get; set; }
}
剃须刀页面中的下拉列表如下所示:
@model ICollection<Lab.Quiz.BL.Services.TestCardService.Models.TestQuestionModel>
...
@Html.DropDownList("QuestionType", new SelectListItem[]
{
new SelectListItem() {Text = "Single Selection Question", Value = "SingleSelectionQuestion", Selected = true},
new SelectListItem() {Text = "Multiple Answers Question", Value = "MultipleAnswerQuestion"},
new SelectListItem() {Text = "Open Answer Question", Value = "OpenAnswerQuestion"},
new SelectListItem() {Text = "Program Code Question", Value = "ProgramCodeQuestion"}
},
new
{
onchange = "filterQuestions('{@Model.FirstOrDefault()?.TestId.ToString()}', this.value)"
})
索引页面上的功能是这样的:
function filterQuestions(testId, questionType) {
alert("called");
$.ajax({
url: "Home/Filter",
data: { testId: testId, questionType: questionType },
type: 'GET',
dataType: 'html',
success: function (result) {
$("#questionsDiv").html(result);
}
});
很遗憾,该函数不会被调用。但是,如果我将函数更改为仅接受一个属性,并相应地将函数调用更改为仅包含一个属性 (this.value),它会按预期工作。 关于如何从@Html.DropdownList 传递它的值以及模型的属性的任何建议?
【问题讨论】:
-
你能检查生成的 HTML 结果并发布它,也许那个语法有问题吗?
-
到目前为止,我用原始 HTML 代码替换了 HTML 帮助程序,并设法将选定的选项值和 testId 都传递给了控制器。问题一定是我在 htmlhelper 中使用的语法,即在 ... onchange = "filterQuestions('{@Model.FirstOrDefault()?.TestId.ToString()}', this.value)"
-
当然问题出在这个语法上。检查你的浏览器,检查生成的
-
@Razr_Max 你对我的帖子有什么问题吗?如果您有任何疑问,请随时提问。如果它有帮助,你可以接受作为答案,这样它可以帮助更多的人。
标签: javascript asp.net function asp.net-core html.dropdownlistfor