我遇到了外部API数据提取问题,其中有2个<select>
输入从URL中获取数据/jobs/dataId=44.
因此,当提取此URL时,特定于dataId=44
的选项会填充这两个下拉列表。
我通过控制器过滤此数据,该控制器根据通过GET请求传递的dataId
进行服务调用。
控制器:
public JsonResult GetFilteredData(int? id, string term, int? page)
{
NewRelic.Api.Agent.NewRelic.AddCustomParameter("params", JsonConvert.SerializeObject(new { ID = id, Term = term, Page = page }));
IEnumerable<JobChoice> choices;
if (id.Value == JobQuestionID)
{
var work_places = GetWorkMatches(term, null);
choices = work_places.Select(x => new JobChoice { ChoiceText = x.Name, Id = Convert.ToUInt32(x.ChoiceID) });
}
else
{
// This is where I'm writing a condition to filter the dropdowns
if (id == 44)
{
// Only show Supported Jobs
var jobcodes = new List<uint> {1,2,3,4,5};
choices = _jobAdapter.FetchQuestionChoices(id.Value)
.Select(x => _jobChoiceTransformer.Transform(x))
.Where(x => x != null && !jobcodes.Contains(x.Id));
}
else
{
var searchTerm = JobChoiceConverter.Scrub(term);
choices = _jobAdapter.FetchQuestionChoices(id.Value)[searchTerm]
.Select(x => _jobChoiceTransformer.Transform(x))
.Where(x => x != null);
if (!string.IsNullOrWhiteSpace(searchTerm))
{
var jobChoices = choices.OrderBy(x => x.ScrubbedChoiceText);
var choicesThatStartWithTerm = jobChoices.Where(x => x.ScrubbedChoiceText.StartsWith(searchTerm)).ToList();
var choicesThatContainTerm = jobChoices.Where(x => x.ScrubbedChoiceText.Contains(searchTerm) && !choicesThatStartWithTerm.Contains(x));
choices = choicesThatStartWithTerm.Concat(choicesThatContainTerm);
}
}
}
var choicesArray = choices.ToArray();
if (!page.HasValue)
return PagedJson(choicesArray, 1, Int32.MaxValue);
return PagedJson(choicesArray, page.Value, PageSize);
}
因此,您可以看到问题是这两个下拉列表在发出GET请求时共享相同的Id。我只是想知道在这种情况下是否有一些聪明的方法来编写IF条件,我可以过滤一个下拉列表而不是另一个下拉列表。
修改
客户端代码使用CoffeeScript生成下拉框。这是代码的样子:
CoffeeScript的:
new Question.JobChoice
name: 'Jobs'
questionText: 'Select jobs that you have.'
maximumSelectionSize: 5
questionID: 44
new Question.JobChoice
name: 'SupportedPrimaryJob'
questionText: 'What is your primary job (required)?'
maximNCumSelectionSize: 1
choiceID: 'Primary_Job_Choice'
skipLogic: (context) -> PrimaryJobSkip(context)
validateLogic: (validator, context) -> PrimaryJobValidate(validator, context, 'Primary Job Required')
questionID: 44
非常感谢任何帮助!
答案 0 :(得分:0)
所以我解决这个问题的方法是在这个构造函数中添加一个额外的参数,然后我最终将它传递给URL。一旦我将其传递到URL,我就可以通过GET请求获取此附加参数,并区分2个下拉字段。