我正在构建一个需要用户回答调查的应用程序。 我已经建立了调查,以处理多选题的问题;但是,我在使用免费回答问题时也遇到了麻烦。
这是我在视图中的表单:
@using (Html.BeginForm("Submit", "Survey", FormMethod.Post, new { role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary("", new { @class = "text-danger" })
var questionNumber = 0;
<h4>Matchmaking Questions</h4>
foreach (var question in Model.Questions)
{
var answers = Model.Answers.Where(a => a.QuestionId == question.Id);
var answerList = new List<Answer>();
<div class="form-group">
<h5>
<i>Q #@(questionNumber + 1)</i>:
@if (question.SurveyId == 3)
{
if (User.IsInRole(UserType.HOST_ROLE))
{
<span>@question.Text</span>
}
else
{
<span>@question.Text2</span>
}
}
else
{
<span>@question.Text</span>
}
</h5>
<!-- If a question has a comment, display it here -->
@if (!question.Comment.IsNullOrWhiteSpace())
{
<h6>@question.Comment</h6>
}
@if (answers.ToList().Any())
{
answerList = answers.ToList();
@Html.DropDownListFor(m => m.UserAnswerIds[questionNumber],
new SelectList(answerList, "Id", "Text"),
"Select your answer", new { @class = "form-control" })
}
else
{
// TextAnswers has the correct number of count here...
// But the values are not updated...
Model.TextAnswers.Add(questionNumber, "");
@Html.TextAreaFor(m => m.TextAnswers[questionNumber], 5, 300, new { })
}
</div>
questionNumber++;
}
<div class="form-group">
<div class="col-md-offset-5 col-md-12">
<input type="submit" class="btn btn-primary" value="Submit" />
</div>
</div>
}
这是我的视图模型:
public class SurveyViewModel
{
public IEnumerable<Question> Questions { get; set; }
public IEnumerable<Answer> Answers { get; set; }
public ApplicationUser User { get; set; }
public int[] UserAnswerIds { get; set; }
public Dictionary<int, string> TextAnswers { get; set; }
}
这是我提交表格的行动:
public ActionResult Submit(SurveyViewModel viewModel)
{
var userAnswerIds = viewModel.UserAnswerIds;
var userId = User.Identity.GetUserId();
var surveyComplete = true;
// Create a new answer based on user's text
foreach (var textAnswer in viewModel.TextAnswers)
// viewModel.TextAnswers has count of 0 here...
{
var customAnswer = new Answer()
{
QuestionId = textAnswer.Key,
Text = textAnswer.Value
};
try
{
customAnswer = _context.Answers.Add(customAnswer);
}
catch (Exception e)
{
Console.WriteLine(e);
}
// Add the new custom answer to user's list of answers
userAnswerIds[textAnswer.Key] = customAnswer.Id;
}
// Add each answer to UserAnswer
foreach (var t in userAnswerIds)
{
var currentAnswerBlock = t;
if (_context.Answers.Any(a => a.Id == currentAnswerBlock))
{
var ua = new UserAnswer()
{
ApplicationUserId = userId,
AnswerId = t
};
try
{
_context.UserAnswers.Add(ua);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
else
{
surveyComplete = false;
}
}
// Update user's survey completion status
try
{
_context.Users.Single(u => u.Id == userId).SurveyComplete = surveyComplete;
}
catch (Exception e)
{
Console.WriteLine(e);
}
_context.SaveChanges();
return RedirectToAction("Index", "Profile");
}
我遇到的问题是,当我尝试使用控制器中的TextAnswer字典时,由于某种原因,字典似乎是空的。当我尝试调试时,字典在View中工作,“TextAreaFor”方法似乎没有更新字典的值。但是,在渲染视图期间,字典就在那里,并且为每个文本答案创建了条目:它只是值不会更新,也不会与视图模型一起传递到控制器中。
我完全难过,并且会喜欢专业人士的指导。