MVC4复杂的viewmodel单选按钮验证集

时间:2013-05-28 21:17:04

标签: c# asp.net-mvc asp.net-mvc-4

给出下面列出的简单MVC应用程序:

1)这是一种合适的方法吗?这看起来很简单,但我不确定在这种情况下你是如何使用控制器的,尤其是这部分:

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

2)我如何完成客户端验证?我是否必须按列出的in this MSDN article实现IValidatableObject,或者有更简单的方法吗?

控制器:

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>
}

1 个答案:

答案 0 :(得分:0)

required=""
添加到
@Html.RadioButton("Answers[" + i + "].AnswerValue", label.Value, false, new { id = "q" + i + "_a" + label.Label, required="" })@

中的HtmlAttributes