我正在用ASP开发一个小调查。NET MVC。我创建了我的模型问题、问题类别、回答、回答类别,并用种子数据填充它们。
问题模型的索引视图工作得很好,但我需要添加一个额外字段,该字段将包含每个问题的下拉列表,用户可以使用下拉列表中选择的选项回答问题。
下面是我的问题模型
public class Question
{
[Key]
public byte Id { get; set; }
[Required]
[Display(Name = "Survey Question")]
public string SurveyQuestion { get; set; }
public QuestionCategory QuestionCategory { get; set; }
[Display(Name = "Question Category")]
public byte QuestionCategoryId { get; set; }
}
public class QuestionCategory
{
public byte Id { get; set; }
public string QuestionCat { get; set; }
}
这是响应模型
public class Response
{
public byte Id { get; set; }
public string ResponseName { get; set; }
public byte Weight { get; set; }
public ResponseCategory ResponseCategory { get; set; }
public byte ResponseCategoryId { get; set; }
}
public class ResponseCategory
{
public byte Id { get; set; }
public string ResponseCat { get; set; }
}
public ActionResult Index()
{
var questions = _context.Question.Include(q => q.QuestionCategory).ToList();
return View(questions);
}
最后,下面是显示问题的视图
@model IEnumerable<WorkPlaceBullying.Models.Question>
@{
ViewBag.Title = "Survey Questions";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Questions</h2>
@if (!Model.Any())
{
<p>We don't have any Questions yet!.</p>
}
@Html.ActionLink("Question", "New", "Question", "", new { @class = "btn btn-primary" })
<table class="table table-striped table-hover ">
<thead>
<tr>
<td>ID</td>
<td>Questions</td>
<td>Response Category</td>
</tr>
</thead>
@foreach (var question in Model)
{
<tr>
<td>@question.Id</td>
<td>
@Html.ActionLink(@question.SurveyQuestion, "Edit", "Question", new { id = @question.Id }, null)
</td>
<td>
@question.QuestionCategory.QuestionCat
</td>
<td>
@*@Html.DropDownListFor(q => q.Question.QuestionCategoryId, new SelectList(Model.QuestionCategory, "Id", "QuestionCat"), "Select Question Category", new { @class = "form-control" })*@
</td>
</tr>
}
</table>
我似乎无法在dropdownlist中找到问题控制器索引视图中的答案。我们将不胜感激。