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", );
            }
HomeMade
  • 552
  • 4
  • 10
  • 21
  • Where are your validation rules? Is it just the required fields? Please post your controller code. – melkisadek Sep 21 '19 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 – HomeMade Sep 21 '19 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. – melkisadek Sep 21 '19 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. – HomeMade Sep 21 '19 at 16:20
  • I will update the code, the submit controller does nothing for now except checking if the model is valid – HomeMade Sep 21 '19 at 16:21
  • I don't think you can access the validation annotations of a child object from the validation context. You'd need to write a custom validator to do it this way. [This might help.](https://stackoverflow.com/a/33631861/50179) – melkisadek Sep 21 '19 at 16:29
  • I am confused why not because it renders 'em inside span these messages, it just doesn't react to display these on submit. – HomeMade Sep 21 '19 at 16:31
  • I've added props inside Question [Required] public int resultID { get; set; } [Required(AllowEmptyStrings = false)] public string resultValue { get; set; } and still the same, it's not working. – HomeMade Sep 21 '19 at 16:35
  • I've reproduced your code and it appears to be working as intended. Are you getting errors or is there something specific not happening? When you click Submit you go to the POST action in the controller and it validates the model. If the Result field is left blank then it returns "The Text field is required." – melkisadek Sep 21 '19 at 18:01
  • Oh God, I don't receive that error message :(. What could be set-up inside the project so that it overrides that message from appearing? – HomeMade Sep 21 '19 at 18:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199775/discussion-between-homemade-and-melkisadek). – HomeMade Sep 21 '19 at 18:46

1 Answers1

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>
}
melkisadek
  • 1,043
  • 1
  • 14
  • 33
  • I see it same as in your post, it's before – HomeMade Sep 21 '19 at 18:45
  • 1
    The code you posted above shows two braces above the submit div. Can you copy and paste your exact code over the original post. – melkisadek Sep 21 '19 at 19:00
  • 1
    You also have a #1 where there should probably be a letter i. `@Html.HiddenFor(m => m[1].Result.ResultID )` – melkisadek Sep 21 '19 at 19:12
  • 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! – HomeMade Sep 21 '19 at 20:22