I'm working on some back-end code
Take a look at the code snippet below
[HttpGet]
public ActionResult GetQuestions()
{
var _id = TempData["ID"];
var questBlock = db.QuestionBlocks
.Where(x => x.Interview_Id == (int?) _id)
.Select(x => new
{
ID = x.Block_ID,
Question1 = x.Question1,
Question2 = x.Question2,
Question3 = x.Question3,
Question4 = x.Question4,
Question5 = x.Question5,
Question6 = x.Question6,
Question7 = x.Question7,
Question8 = x.Question8,
Question9 = x.Question9,
Question10 = x.Question10,
})
.ToList();
return Json(questBlock, JsonRequestBehavior.AllowGet);
}
Some questions may not have values.
On the front-end side, I display these questions in the View
<script>
$(document).ready(function() {
loadQuestionBlock();
});
function loadQuestionBlock() {
$.ajax({
url: '@Url.Action("GetQuestions", "Interview")',
contentType: 'application/json; charset=utf-8',
type: 'GET',
dataType: 'json',
processData: false,
success: function(result) {
var email = result;
for (var i = 0; i <= email.length - 1; i++) {
var question =
'<div class="activeQue" style="font-size:20px;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);">' +
email[i].Question1 +
'</div>' +
// additional questions ...
$("#questions").append(question);
$('.hiddenQue').each(function() {
if ($(this).text().trim() === '') {
$(this).hide();
}
});
Above, I used
$('.hiddenQue').each(function() {if ($(this).text().trim() === '') {$(this).hide();}});
to achieve this functionality. Is there a better way to accomplish it?