发布

时间:2018-03-18 17:39:15

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

我想确保用户选择问题的答案。我尝试过JQuery,Custom Validation类,将模型属性设置为必需。总而言之,如果没有选择单选按钮,则会显示错误消息,用户必须在进入下一个问题之前选择一个答案。

这是没有ValidationMessageFor的HTML / Razor,因为它没有工作。

@using (Html.BeginForm("Question", "PreRegistration", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
 @if (Model.Questions.fldAnswerType == "Radio")
  {
    <form id="myForm" name="myForm">
        <div class="form-group">
            <div style="margin-left: 20px;">
                @{
                    if (Model.Questions.fldOption1 != null)
                    {
                        <div class="radio">
                            @Html.RadioButton("Radio", @Model.Questions.fldOption1, new { @class = "myGroup" }) @Model.Questions.fldOption1                             
                        </div>
                    }

                    if (Model.Questions.fldOption2 != null)
                    {
                        <div class="radio">
                            @Html.RadioButton("Radio", @Model.Questions.fldOption2, new { @class = "myGroup" }) @Model.Questions.fldOption2                             
                        </div>
                    }

                    if (Model.Questions.fldOption3 != null)
                    {
                        <div class="radio">
                            @Html.RadioButton("Radio", @Model.Questions.fldOption3, new { @class = "myGroup" }) @Model.Questions.fldOption3
                        </div>
                    }

                    if (Model.Questions.fldOption4 != null)
                    {
                        <div class="radio">
                            @Html.RadioButton("Radio", @Model.Questions.fldOption4) @Model.Questions.fldOption4
                        </div>
                    }

                    if (Model.Questions.fldOption5 != null)
                    {
                        <div class="radio">
                            @Html.RadioButton("Radio", @Model.Questions.fldOption5) @Model.Questions.fldOption5
                        </div>
                    }

                    if (Model.Questions.fldOption6 != null)
                    {
                        <div class="radio">
                            @Html.RadioButton("Radio", @Model.Questions.fldOption6) @Model.Questions.fldOption6
                        </div>

                    }
                }
            </div>
        </div>
    </form>
<input type="submit" value="Next" id="btn" class="btn btn-primary" />

我花了一整天时间试图让它发挥作用,但我无法......

[HttpPost]
public ActionResult Question(FormCollection collection, Questions questions)
{
   if (!ModelState.IsValid)
   {
     var viewModel = new RegistrationViewModel
      {
        Questions = questions,
        Responses = response
      };

     return View("Question", viewModel);
   }
 }

模型我回发的问题总是为空,所以我假设这就是模型状态始终有效的原因?

任何指针都将不胜感激

1 个答案:

答案 0 :(得分:-1)

试试这个:

HttpPost] public ActionResult Question(FormCollection collection, Questions questions)
{
    if (ModelState.IsValid)
    {
        var viewModel = new RegistrationViewModel { Questions = questions, Responses = response };
        return View("Question", viewModel);
    }
}
Response.next page