2012-05-03 69 views
1

與單選工作,我有模型在視圖和控制器使用ASP.NET

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> 

UPDATE

我改變我的觀點和模型,但我沒有正確的答案

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變量列表答案是空的。 我的麻煩在哪裏?

回答

0

模型聯編程序無法創建IEnumerable類型,因此您必須將其更改爲List或Collection

相關問題