【问题标题】:Trouble creating DropdownListFor with Razor使用 Razor 创建 DropdownListFor 时遇到问题
【发布时间】:2014-03-21 15:38:42
【问题描述】:

我正在尝试在我的表单上创建一个选择列表。我正在使用 Razors Html.DropDownListFor,但我不确定如何使用模型中的适当值正确填充 DropDownList。

for (int i = 0; i < Model.Questions.Count(); i++)
{
   for (int j = 0; j < Model.Questions[i].Options.Count(); j++)
   {
      @Html.DropDownListFor(m=>m.Questions[i].Options[j].IsSelected, ????)
   }
}

型号:

public class QuestionViewModel
{
    public int? Id { get; set; }

    public string QuestionType { get; set; }

    public string SubType { get; set; }

    public string Text { get; set; }

    public int SortOrder { get; set; }

    public bool IsHidden { get; set; }

    public List<QuestionOptionViewModel> Options { get; set; }    
}

public class QuestionOptionViewModel
{
    public int? Id { get; set; }

    public string Text { get; set; }

    public string Value { get; set; }

    public bool IsChecked { get; set; }

    public int Selected { get; set; }
}

【问题讨论】:

    标签: c# asp.net-mvc razor


    【解决方案1】:

    检查以下解决方案。我稍微改变了你的模型 -

    public class QuestionMainModel
    {
        public List<QuestionViewModel> Questions { get; set; }
    }
    
    public class QuestionViewModel
    {
        public int? Id { get; set; }
        public string Text { get; set; }
        public List<QuestionOptionViewModel> Options { get; set; }
        public int Selected { get; set; }
    }
    public class QuestionOptionViewModel
    {
        public int? Id { get; set; }
        public string Text { get; set; }
        public string Value { get; set; }        
    }
    

    填充视图的控制器动作 -

        public ActionResult Index()
        {
            QuestionMainModel model = new QuestionMainModel();
    
            List<QuestionOptionViewModel> options = new List<QuestionOptionViewModel>();
            options.Add(new QuestionOptionViewModel(){ Id = 1, Text = "Ans1", Value = "1"});
            options.Add(new QuestionOptionViewModel(){ Id = 2, Text = "Ans2", Value = "2"});
            options.Add(new QuestionOptionViewModel(){ Id = 3, Text = "Ans3", Value = "3"});
    
            model.Questions = new List<QuestionViewModel>();
            model.Questions.Add(new QuestionViewModel() { Id = 1, Text = "Question1", Options = options });
    
            return View(model);
        }
    

    正在显示的视图如下 -

    @model MVC.Controllers.QuestionMainModel
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    @using (Html.BeginForm("Submit", "sample", FormMethod.Post))
    {
    
        for (int i = 0; i < Model.Questions.Count; i++)
        {
            @Html.HiddenFor(m => m.Questions[i].Id)
            @Html.LabelFor(m => m.Questions[i].Text, Model.Questions[i].Text)
            @Html.DropDownListFor(m => m.Questions[i].Selected, new SelectList(Model.Questions[i].Options, "Value", "Text"), "Select an option")
        }
    
        <input type="submit" value="Click" />
    }
    

    当你点击提交按钮时,它会点击下面的控制器动作 -

        public ActionResult Submit(QuestionMainModel model)
        {
            return null;
        }
    

    选择的答案如下 -

    注意:如果您想发布所有选项和问题文本,请使用 HiddenFields,就像我为 Is 所做的那样。

    【讨论】:

    • 我真的不需要对我的模型进行任何重大修改。我真的接受了你为 DropDownListFor 所拥有的东西,而且它几乎可以工作。 @Html.DropDownListFor(m => m.Questions[i].Selected, new SelectList(Model.Questions[i].Options, "Id", "Text"),new {@class= "form-control"})
    • @allencoded,希望是的,试一试。但是您可能需要稍微更改一下 Razor 代码。
    • 啊,没有。我想我可能不得不为此打开另一个问题
    • @allencoded,可能是的,您可能需要再提出一个问题。我也会留意你的新问题:-)。
    【解决方案2】:

    您可以尝试以下方法:

    for (int i = 0; i < Model.Questions.Count(); i++)
    {
       @Html.DropDownListFor(m => 
           m.Questions[i].Id,
           new SelectList(
               m.Questions[i].Options, 
               "Text",
               "Value",
               m.Questions[i].Options.SingleOrDefault(x = > x.IsChecked).Value))
    }
    

    但请记住,您应该只有一个选项将 IsChecked 标志设置为 true。否则你会收到一个错误。

    【讨论】:

    • 这样做会返回错误。对象引用未设置为对象的实例
    猜你喜欢
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多