0

I have a view and a model, the model contains questions and a prop to store result for the particular question. I cannot get it to validate the questions, it always passes the Model validation test on submitting (inside the controller). I checked that jquery, jqueryvalidate and jqueryunobrtusive are both included in that order, I tried moving Summary inside and outside the form, didn't help. It appears after submit as if all is valid and messages are rendered upon rendering of the view.

public class Result
    {
        [Required]
        public int ResultID { get; set; }

        [Required]
        public string Text { get; set; }
    }

 public class Question
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public Result Result{ get; set; }
    }

<div>
    @Html.ValidationSummary(true, "", new { @class = "text-danger" });
</div>

@using (Html.BeginForm("Submit", "post", FormMethod.Post, new { id = "Form" }))
{
    HtmlHelper.ClientValidationEnabled = true;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

    for (int i = 0; i < Model.Count; i++)
    {
        var item = Model[i];

            <div>

                <div>
                    @item.Text
                    @Html.TextBoxFor(m => m[i].Result.Text, null, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m[i].Result.Text)
                    @Html.HiddenFor(m => m[1].Result.ResultID )
                </div>
            </div>
        }
    }

    <div>
        <input type="submit" value="Submit"/>
    </div>
}
       public ActionResult Index()
       {           
           List<Question> Questions = GetAllQuestions();

           return View("View", Questions);
       }

        public ActionResult Submit(List<Question> sQuestions)
        {

            if (!ModelState.IsValid)
            {
                return View("Index", );
            }
11
  • Where are your validation rules? Is it just the required fields? Please post your controller code. Commented Sep 21, 2019 at 16:06
  • I have only required fields, just to make it work, and then I would add more later on. The controller is simple, updated the code Commented Sep 21, 2019 at 16:12
  • Are you expecting the validation to occur client-side (in which case you need javascript based validation rules) or server-side (in which case you need to show your controller's POST method. Commented Sep 21, 2019 at 16:15
  • I need both Client side and backend validation. I understood from many posts and articles that jquery unobtrusive will take the backend validation defined on Model props and validate on the client side, without the need to write my own. Commented Sep 21, 2019 at 16:20
  • I will update the code, the submit controller does nothing for now except checking if the model is valid Commented Sep 21, 2019 at 16:21

1 Answer 1

0

It looks like your submit button isn't within your form.

Remove the extra closing brace and it should work. Sorry for not spotting that earlier. I just did a quick code comparison.

View should look like this:

@using (Html.BeginForm("Submit", "Home", FormMethod.Post, new { id = "Form" }))
{
    HtmlHelper.ClientValidationEnabled = true;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

    for (int i = 0; i < Model.Count; i++)
    {
        var item = Model[i];

        <div>

            <div>
                @item.Text
                @Html.TextBoxFor(m => m[i].Result.Text, null, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m[i].Result.Text)
                @Html.HiddenFor(m => m[i].Result.ResultID)
            </div>
        </div>
    }
    <div>
        <input type="submit" value="Submit" />
    </div>
}
Sign up to request clarification or add additional context in comments.

4 Comments

I see it same as in your post, it's before
The code you posted above shows two braces above the submit div. Can you copy and paste your exact code over the original post.
You also have a #1 where there should probably be a letter i. @Html.HiddenFor(m => m[1].Result.ResultID )
Yea, I've fixed that, and the curly braces. I cannot post the code, it's proprietary but I've mostly transfers what's behind. I've solved this by updating the version of all jquery validation scripts and now it shows errors. Thanks a bunch man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.