如何使用我的模型中的适当值正确填充我的无线电?

时间:2014-02-20 19:16:18

标签: c# asp.net-mvc razor

我正在尝试在表单上创建一组单选按钮。我正在使用Razors Html.RadioButtonFor,但我不确定如何使用我的模型中的适当值正确填充我的Radios。

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

你可能会注意到我正在尝试做同样的事情你可以在下拉列表中做,因为它们基本上是一样的。

模型:

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 int Selected { 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; }
    }

1 个答案:

答案 0 :(得分:3)

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

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)
        for (int j = 0; j < Model.Questions[i].Options.Count; j++)
        {
            @Html.RadioButtonFor(m => m.Questions[i].Selected, Model.Questions[i].Options[j].Value) @Model.Questions[i].Options[j].Text
        }
    }

    <input type="submit" value="Click" />
}

当您点击提交按钮时,它会点击控制器操作 -

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

所选答案如下 -

enter image description here

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