2013-05-28 2 views
1

с использованием простого приложения MVC, приведенного ниже:MVC4 комплексная модель смены кнопок для радиомодуля

1) Это подходящий подход? Это кажется достаточно простым, но я не уверен, что это каким образом вы используете контроллер в этом случае, в частности, эта часть:

[HttpPost] 
public ActionResult ListOfQuestions(QuestionViewModel qvm) 
{ 
    if (ModelState.IsValid) 
     return RedirectToAction("Thanks", qvm); 
    else 
     return ListOfQuestions(); 
} 

2) Как я выполняю на стороне клиента проверки? Должен ли я реализовать IValidatableObject, как указано in this MSDN article, или есть более простой способ сделать это?

Контроллер:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; 

     return View(); 
    } 

    public ActionResult ListOfQuestions() 
    { 
     var qvm = new QuestionViewModel(); 

     qvm.Questions.Add(new Question { Id = 1, QuestionText = "This is Question 1" }); 
     qvm.Questions.Add(new Question { Id = 2, QuestionText = "This is Question 2" }); 
     qvm.Questions.Add(new Question { Id = 3, QuestionText = "This is Question 3" }); 
     qvm.Questions.Add(new Question { Id = 4, QuestionText = "This is Question 4" }); 

     qvm.AnswerOptions.Add(new Segment { Label = "1", Value = "1" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "2", Value = "2" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "3", Value = "3" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "4", Value = "4" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "5", Value = "5" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "6", Value = "6" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "7", Value = "7" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "8", Value = "8" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "9", Value = "9" }); 
     qvm.AnswerOptions.Add(new Segment { Label = "10", Value = "10" }); 

     return View(qvm); 
    } 

    [HttpPost] 
    public ActionResult ListOfQuestions(QuestionViewModel qvm) 
    { 
     if (ModelState.IsValid) 
      return RedirectToAction("Thanks", qvm); 
     else 
      return ListOfQuestions(); 
    } 

    public ActionResult Thanks() 
    { 
     return View(); 
    } 
} 

вид модели:

public class QuestionViewModel 
{ 
    public List<Question> Questions { get; set; } 
    public List<Answer> Answers { get; set; } 
    public List<Segment> AnswerOptions { get; set; } 

    public QuestionViewModel() 
    { 
     Questions = new List<Question>(); 
     Answers = new List<Answer>(); 
     AnswerOptions = new List<Segment>(); 
    } 
} 

public class Segment 
{ 
    public string Label { get; set; } 
    public string Value { get; set; } 
} 

public class Answer 
{ 
    public int QuestionId { get; set; } 
    [Required] 
    public string AnswerValue { get; set; } 
} 

public class Question 
{ 
    public int Id { get; set; } 
    public string QuestionText { get; set; } 
} 

Вид:

@model MvcApplication2.Models.QuestionViewModel 

@{ 
    ViewBag.Title = "ListOfQuestions"; 
} 

<h2>ListOfQuestions</h2> 
<br /> 
<br /> 
<br /> 
@using (Html.BeginForm()) 
{ 
    for (int i = 0; i < Model.Questions.Count; i++) 
    { 
    <input type="hidden" name="Answers[@i].QuestionId" value="@Model.Questions[i].Id" /> 
    <strong>The Question:</strong> @Model.Questions[i].QuestionText <br /> 

     foreach (var label in Model.AnswerOptions) 
     { 
    @Html.RadioButton("Answers[" + i + "].AnswerValue", label.Value, false, new { id = "q" + i + "_a" + label.Label })@Html.Label("q" + i + "_a" + label.Label, label.Value) 

     } 
    <br /> 
    @Html.ValidationMessage("Answers[" + i + "].AnswerValue") 

    <br /> 
    <br /> 

    } 

    <button type="submit" value="Submit Data">Submit Data</button> 
} 

ответ

0

Добавить

required=""
в HtmlAttributes в вашем
@Html.RadioButton("Answers[" + i + "].AnswerValue", label.Value, false, new { id = "q" + i + "_a" + label.Label, required="" })@