Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Razor创建DropDownList时遇到问题_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 使用Razor创建DropDownList时遇到问题

C# 使用Razor创建DropDownList时遇到问题,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我正在尝试在表单上创建一个选择列表。我正在使用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.Q

我正在尝试在表单上创建一个选择列表。我正在使用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, ????)
   }
}
for(int i=0;im.Questions[i]。选项[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; }
}
公共类问题视图模型
{
公共int?Id{get;set;}
公共字符串QuestionType{get;set;}
公共字符串子类型{get;set;}
公共字符串文本{get;set;}
公共int排序器{get;set;}
公共bool ishiden{get;set;}
公共列表选项{get;set;}
}
公共类问题选项视图模型
{
公共int?Id{get;set;}
公共字符串文本{get;set;}
公共字符串值{get;set;}
已检查公共布尔值{get;set;}
选定的公共int{get;set;}
}

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

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; }        
}
所选答案如下:

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


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

您可以尝试以下方法:

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))
}
for(int i=0;i
m、 问题[i].Id,
新选择列表(
m、 问题[i].选项,
“文本”,
“价值”,
m、 问题[i].Options.SingleOrDefault(x=>x.IsChecked.Value))
}

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

执行此操作将返回错误。未设置为对象实例的对象引用实际上不需要对我的模型进行任何重大编辑。我真的拿了你的DropDownListFor的东西,而且几乎可以用@Html.DropDownListFor(m=>m.Questions[i]。选中,新建选择列表(Model.Questions[i]。选项,“Id”,“Text”),新建{@class=“form control”}@allencoded,希望是的,请尝试一下。但是你可能需要修改一下Razor代码。啊,它没有。我想我可能得为你再问一个问题that@allencoded,可能是的,您可能需要再问一个问题。我也会关注你的新问题:-)。
    public ActionResult Submit(QuestionMainModel model)
    {
        return null;
    }
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))
}