0

Lets say I have a form with no model binding or data annotations. When the form is posted how can I return the view with a validation message beneath the control - Note I'm trying to do server side validation here?

Below is a kind of example.

<input name="Address" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="Address" data-valmsg-replace="true"></span>


 public ActionResult Create(FormCollection collection)
 {
        if (string.IsNullOrEmpty(collection["Address"])
        {
            // Set the field validation error span message
            ModelState.AddModelError("Address", "This field is required.");

            return View();
        }   
 }      

Note: I know how add validation using a view model and data annotations. In this scenario I'm unable to use a view model so need some way to manually validate and return the validation messages back to the view.

The above doesn't seem to work

* Update *

Perhaps using viewData as follows:

<span class="field-validation-valid" data-valmsg-for="Address" data-valmsg-replace="true">@ViewData["Address"]</span> 
13
  • 1
    Have your tried adding @Html.ValidationMessageFor(m => m.Address, "") beneath the control? This should display all model state errors added in your controller for that property. Also there is @Html.ValidationSummary() which displays all model state errors. Commented Oct 17, 2017 at 16:54
  • Do what zgood said and also return your model to your view. return View(collection) Commented Oct 17, 2017 at 16:55
  • Also, don't forget to separate your ActionResults for your Get and Post, not sure if you are trying to combine both here. Commented Oct 17, 2017 at 16:57
  • Last bit for me, in this instance you should put the [Required] data attribute above your Address property in your model and let the asp.net engine handle this type of validation for you. Commented Oct 17, 2017 at 16:59
  • 1
    @adam78 Well, you could cram all your validation errors in a Dictionary or some type of collection and put that in the ViewBag or whatever, then iterate through that in the View. But without a model all of the seems off. How will you display the values back on the form when it fails validation? It would be a blank form with just validation errors and the user would have to fill out all the fields again. Commented Oct 17, 2017 at 18:51

1 Answer 1

1

I would go with html helpers.

@Html.ValidationMessage("Address") 

This will automatically generate the HTML:

<span class="field-validation-valid" data-valmsg-for="address" data-valmsg-replace="true"></span>`

Your code looks correct.

if (string.IsNullOrEmpty(collection["Address"]))
{
  // Set the field validation error span message
  ModelState.AddModelError("Address", "This field is required.");
 return View();
}
Sign up to request clarification or add additional context in comments.

Comments

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.