麻烦剃刀创造DropdownListFor [英] Trouble creating DropdownListFor with Razor

查看:103
本文介绍了麻烦剃刀创造DropdownListFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建的窗体上选择列表。我使用的剃须刀Html.DropDownListFor,但我不确定如何正确填充我的DropDownList从我的模型的相应值。

 的for(int i = 0; I< Model.Questions.Count();我++)
{
   对于(INT J = 0; J< Model.Questions [I] .Options.Count(); J ++)
   {
      @ Html.DropDownListFor(M = GT; m.Questions [I]可供选项[J] .IsSelected,????)
   }
}

模型:

 公共类QuestionViewModel
{
    公众诠释?标识{搞定;组; }    公共字符串QuestionType {搞定;组; }    公共字符串子类型{搞定;组; }    公共字符串文本{搞定;组; }    公众诠释SortOrder的{搞定;组; }    公共BOOL是否隐藏{搞定;组; }    公开名单< QuestionOptionViewModel>选项​​{搞定;组; }
}公共类QuestionOptionViewModel
{
    公众诠释?标识{搞定;组; }    公共字符串文本{搞定;组; }    公共字符串值{获得;组; }    公共BOOL {器isChecked获得;组; }    公众诠释选择{搞定;组; }
}


解决方案

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

 公共类QuestionMainModel
{
    公开名单< QuestionViewModel>问题{搞定;组; }
}公共类QuestionViewModel
{
    公众诠释?标识{搞定;组; }
    公共字符串文本{搞定;组; }
    公开名单< QuestionOptionViewModel>选项​​{搞定;组; }
    公众诠释选择{搞定;组; }
}
公共类QuestionOptionViewModel
{
    公众诠释?标识{搞定;组; }
    公共字符串文本{搞定;组; }
    公共字符串值{获得;组; }
}

控制器动作而填充视图 -

 公众的ActionResult指数()
    {
        QuestionMainModel模式=新QuestionMainModel();        清单< QuestionOptionViewModel>选项​​=新的List< QuestionOptionViewModel>();
        options.Add(新QuestionOptionViewModel(){ID = 1,文本=ANS1,值=1});
        options.Add(新QuestionOptionViewModel(){ID = 2,文本=ANS2,值=2});
        options.Add(新QuestionOptionViewModel(){ID = 3,文本=ANS3,值=3});        model.Questions =新的List< QuestionViewModel>();
        model.Questions.Add(新QuestionViewModel(){ID = 1,文本=问题1,选项=选项});        返回查看(模型);
    }

正被显示的视图是如下 -

  @model MVC.Controllers.QuestionMainModel@ {
    ViewBag.Title =指数;
}< H2>指数< / H>@using(Html.BeginForm(提交,样品,FormMethod.Post))
{    的for(int i = 0; I< Model.Questions.Count;我++)
    {
        @ Html.HiddenFor(M = GT; m.Questions [I] .ID)
        @ Html.LabelFor(M = GT; m.Questions [I]。文本,Model.Questions [I]。文本)
        @ Html.DropDownListFor(M = GT; m.Questions [I] .Selected,新的SelectList(Model.Questions [I]可供选项,值,文本),选择一个选项)
    }    <输入类型=提交值=点击/>
}

当你点击提交按钮,它会触及下方控制器动作 -

 公众的ActionResult提交(QuestionMainModel模型)
    {
        返回null;
    }

和选择的答案会是如下 -

注意:如果您要发布所有的选项和问题文本,然后使用HiddenFields,就像我做的是

I am trying to create a select list on my form. I am using Razors Html.DropDownListFor, but I am uncertain how to properly populate my DropDownList with the appropriate values from my model.

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, ????)
   }
}

MODELS:

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; }
}

解决方案

Check the below solution. I slightly changed your models -

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; }        
}

The controller action which populates the view -

    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);
    }

The view which is being displayed is as follows -

@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" />
}

And when you click submit button, it will hit below controller action -

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

And the selected answer would be as follows -

NOTE: If you want to post all the options and Question Text, then use HiddenFields, just like I did for Is.

这篇关于麻烦剃刀创造DropdownListFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆