这是我的模特
{
public int Id { get; set; }
public int? MainSurveyId { get; set; }
public string Question { get; set; }
public DateTime? DateCreated { get; set; }
public string CreatedBy { get; set; }
public string Status { get; set; }
public IList<xxAnswer> answers { get; set; }
}
这是我的控制者,请注意,我将var question
和var answer
分开了
[HttpPost]
public IActionResult CompareCompetitors(string[] comps, int Surveyid)
{
var model = new CompareCompetitorAnswers();
foreach( var item in comps){
model.competitorNames.Add( new xxCompetitor() { CompetitorName = item} );
}
var questions = _context.SurveyQuestions.ToList().Where(x => x.MainSurveyId == Surveyid);
foreach (var q in questions){
model.questions.Add(new xxQuestion { Question = q.Question});
}
var answers = _context.SurveyCompetitorAnswer.ToList().Where(x => x.MainSurveyId == Surveyid);
foreach (var a in answers){
model.answers.Add(new xxAnswer { Answer = a.Answer});
}
return View(model);
//return string.Join(",", comps); // check if it works with this. ph wait. i think correct. sorry. i will try now.
}
这是我的视图,但在@foreach (var a in q.answers)
中显示错误。
<tbody>
@foreach (var q in Model.questions)
{
<tr>
<td scope="row">
@q.Question
</td>
@foreach (var a in q.answers)
{
<td scope="row">
@a.Answer
</td>
}
</tr>
}
</tbody>
我希望答案贴在结果旁边。它们位于不同的数据库表中。但它在@foreach (var a in q.answers)
处输出错误。
答案 0 :(得分:1)
我终于想出了办法:
FlutterActivity
我没有使用
---
- set_fact:
keys:
- name: user1
keys:
- key1
- key2
- key3
- name: user2
keys:
- key4
- key5
- key6
- name: update authorized keys
authorized_key:
user: "{{ item.0.name }}"
key: "{{ item.1 }}"
state: present
with_subelements:
- "{{ keys }}"
- keys
我刚刚使用过:
<tbody>
@foreach (var q in Model.questions)
{
<tr>
<td scope="row">
@q.Question
</td>
@foreach (var a in Model.answers.Where(x => x.SurveyQuestionId == q.Id))
{
<td scope="row">
@a.Answer
</td>
}
</tr>
}
</tbody>
答案 1 :(得分:0)
我来晚了一点,但是我希望这可以帮助您优化模型方法。
假设这是您的简化模型:
public class AnswerModel
{
// Omitted for simplicity
}
public class QuestionModel
{
public List<AnswerModel> Answers { get; set; } = new List<AnswerModel>();
}
您不应在 .Where()之前执行 .ToList(),因为您实际上是在实现整个实体(即表)。 最好是使用联接进行查询,或者如果愿意,可以使用NavigationProperty(如果有的话)来跟踪与您的问题相关的答案的集合。
但是我将您的方法与两个分开的查询一起使用:
var model = new List<QuestionModel>();
var questions = _context.SurveyQuestions.Where(x => x.MainSurveyId == Surveyid).ToList();
var answers = _context.SurveyCompetitorAnswer.Where(x => x.MainSurveyId == Surveyid).ToList();
foreach (var q in questions){
var qModel = new QuestionModel();
qModel.Answers.AddRange(answers.Where(x => x.SurveyQuestionId == q.Id).ToList());
model.Add(qModel);
}
并使用更简单和汇总的模型:
<tbody>
@foreach (var q in Model)
{
<tr>
<td scope="row">
@q.Question
</td>
@foreach (var a in q.Answers)
{
<td scope="row">
@a.Answer
</td>
}
</tr>
}
</tbody>
如果您要共享您的实体,我们可以优化查询!
祝您编程愉快!