1

我有模特

public class QuestionViewModel
    {
        public string Text { get; set; }
        public int Id { get; set; }
        public bool IsMultiSelected { get; set; }
        public IEnumerable<RadioButtonViewModel> AnswerViewModels { get; set; }
    }
public class RadioButtonViewModel
{
public string Value { get; set; }
public string Name { get; set; }
}

和测试控制器

 [HttpGet]
        public ActionResult ProcessAnswers()
        {
            var question1 = new List<RadioButtonViewModel>
            {
                new RadioButtonViewModel
                {
                    Value = "1",
                    Name = "Radio 1 option 1"
                },
                new RadioButtonViewModel
                {
                    Value = "2",
                    Name = "Radio 1 option 2"
                },
                new RadioButtonViewModel
                {
                    Value = "3",
                    Name = "Radio 1 option 3"
                }
            };
            var question2 = new List<RadioButtonViewModel>
            {
                new RadioButtonViewModel
                {
                    Value = "1",
                    Name = "Radio 2 option 1"
                },
                new RadioButtonViewModel
                {
                    Value = "2",
                    Name = "Radio 2 option 2"
                },
                new RadioButtonViewModel
                {
                    Value = "3",
                    Name = "Radio 2 option 3"
                }
            };
            var qs = new List<QuestionViewModel>
                         {
                             new QuestionViewModel
                                 {
                                     Id = 1,
                                     Text = "Please select correct variant in question 1",
                                     AnswerViewModels = question1
                                 },
                             new QuestionViewModel
                                 {
                                     Id = 2,
                                     Text = "Please select correct variant in question 2",
                                     AnswerViewModels = question2
                                 }
                         };

            return View("TestRadioView", qs);
        }

        [HttpPost]
        public ActionResult ProcessAnswers(List<QuestionViewModel> answers)
        {
            return RedirectToAction("Index");
        }

我的观点

@using System.Web.Mvc.Html
@using ExpertApplication.ViewModels
@model List<QuestionViewModel>
@{
    ViewBag.Title = "Test Radio Button View";

}
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
        @if(Model != null)
        {
            <br />
            using(@Html.BeginForm("ProcessAnswers", "Home", FormMethod.Post))
            {

                foreach(var question in Model)
                {
            <h2>@question.Text</h2>
            <br />
                    foreach(var radioButtonViewModel in question.AnswerViewModels)
                    {
            @Html.RadioButton(question.Id.ToString(), radioButtonViewModel.Value)
            @Html.Label(question.Id.ToString(), radioButtonViewModel.Name)
            <br />
                    }
                }
            <input type="submit" value="Send" />
            }

        }
    </div>
</body>
</html>

更新

我改变了我的观点和模型,但我没有正确的答案

 public IList<RadioButtonViewModel> AnswerViewModels { get; set; }

@using System.Web.Mvc.Html
@using ExpertApplication.ViewModels
@model List<QuestionViewModel>
@{
    ViewBag.Title = "Test Radio Button View";

}
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
        @if(Model != null)
        {
            if(ViewData["Message"] != null)
            {
            <h1>@ViewData["Message"]</h1>
            }
            else
            {
            <h1>Please answer the questions</h1>
            }
            <br />
            using(@Html.BeginForm("ProcessAnswers", "Home", FormMethod.Post))
            {
                for(int i = 0; i < Model.Count(); i++)
                {
                    var question = Model[i];
            <h2>@question.Text</h2>
            <br />
                    for(int j = 0; j < question.AnswerViewModels.Count(); j++)
                    {
                        var radioButtonViewModel = question.AnswerViewModels[j];
            @Html.RadioButton(string.Format("QuestionViewModel[{0}].AnswerViewModels.Id", question.Id), radioButtonViewModel.Value)
            @Html.Label(string.Format("{0}", question.Id), radioButtonViewModel.Name)
            <br />
                    }
                }
            <input type="submit" value="Send" />
            }
        }
    </div>
</body>
</html>

在行动 ProcessAnswers 变量列表答案为空。我的麻烦在哪里?

4

1 回答 1

0

模型绑定器无法创建 IEnumerable 类型,因此您必须将其更改为 List 或 Collection

于 2012-05-03T17:17:10.907 回答