代码之家  ›  专栏  ›  技术社区  ›  Guzzyman

IEnumerable模型中的多个下拉列表

  •  1
  • Guzzyman  · 技术社区  · 7 年前

    我正在用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中找到问题控制器索引视图中的答案。我们将不胜感激。

    2 回复  |  直到 7 年前
        1
  •  2
  •   stomtech    7 年前

    这个 SelectList Constructor

    public static IEnumerable<QuestionCategory> QuestionCategoryList = new List<QuestionCategory> { 
        new QuestionCategory {
            Id = 1,
            QuestionCat = "Red"
        },
        new QuestionCategory {
            Id = 2,
            QuestionCat = "Blue"
        }
    };
    

    将视图中的代码更改为:

    @Html.Hidden(@question.Id + "_QuestionCategoryId", @question.QuestionCategoryId)
    @Html.DropDownList(@question.Id + "_QuestionCategoryId", new SelectList(QuestionCategoryList, "Id", "QuestionCat"), "Select Question Category", new { @class = "form-control", onchange="setQuestionCategoryId(this)" })
    

    您可能需要一些javascript来更新 hidden input OnChange

    jsfiddle 了解如何使用javascript更新QuestionCategoryId值。

    function setQuestionCategoryId(selectObject) 
    {   
       $("input#" + selectObject.id).val(selectObject.value);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input id="1_QuestionCategoryId" name="1_QuestionCategoryId" value="0" />
                
    <select class="form-control" id="1_QuestionCategoryId" name="1_QuestionCategoryId" onchange="setQuestionCategoryId(this)">
        <option>Select Question Category</option> 
        <option value="1">Red</option> 
        <option value="2">Blue</option> 
    </select>
        2
  •  1
  •   Karthik Elumalai    7 年前

    DropDownlist的语法:

    MvcHtmlString Html.DropDownListFor(Expression<Func<dynamic,TProperty>> expression, IEnumerable<SelectLestItem> selectList, string optionLabel, object htmlAttributes)
    

    前任:

    @Html.DropDownListFor(m => m.StudentGender, 
                        new SelectList(Enum.GetValues(typeof(Gender))), 
                        "Select Gender")
    

    在上面的示例中,DropDownListFor()方法中的第一个参数是lambda表达式,该表达式指定要与select元素绑定的模型属性。我们已指定枚举类型的StudentGender属性。第二个参数指定要使用SelectList显示在下拉列表中的项目。第三个参数是optionLabel,它将是dropdownlist的第一项。现在,它生成id为的元素;名称设置为property name-StudentGener和两个列表项-Male&女性,如下所示。

    http://www.tutorialsteacher.com/mvc/htmlhelper-dropdownlist-dropdownlistfor