2

I have a model that has numeric and string attributes, each having the [Required] validation annotation. I also have a respective view that is an input form for this model. For some reason, as soon as the view is loaded, the required validation message for the string attribute shows immediately, as opposed to the required validation message for the numeric attribute which only shows once the user attempts to submit the form (as expected). Does anyone have a clue as to the odd validation behaviour on the string attribute?

Update

I narrowed down the problem to the "complex" way I am showing the view from the controller action. In my case, I have a single controller action, Create, responsible for creating an entity that has many attributes. Since the entity has many attributes, I have split this process into steps through a server side wizard. The following is a simplified version of my controller actions:

public ActionResult Create()
    {
        Model = new CreateEditListingViewModel();
        return View("StepOne");
    }

    [HttpPost]
    public ActionResult Create(string buttonValue, StepOneViewModel stepOneModel, StepTwoViewModel stepTwoModel, StepThreeViewModel stepThreeModel)
    {
        ActionResult nextView = null;
        CreateListingSteps step = (CreateListingSteps)Enum.Parse(typeof(CreateListingSteps), buttonValue);
        // Save the value of the step that has been submitted and redirect user to next step
        switch (step)
        {
            case CreateListingSteps.StepOne:
                Model.StepOne = stepOneModel;
                nextView = View("StepTwo");
                break;
            case CreateListingSteps.StepTwo:
                Model.StepTwo = stepTwoModel;
                nextView = View("StepThree");
                break;
            case CreateListingSteps.StepThree:
                Model.StepThree = stepThreeModel;
                nextView = View("Confirm");
                break;
        }
        return nextView;
    }

Apparently what's happening is that once the user click the Next button the first time (available in all step views), the validation is triggered for all subsequent forms, thus undesireably showing string fields as invalid even if the user hasn't submitted the form yet.

Blank form that hasn't been submitted showing required message for description field.  Price appears regularly because its data type is not string

Can anyone think of a workaround for this?

4
  • Have you initialized your string field to a non-null value? Aug 28, 2012 at 18:13
  • 1
    Do you have any javascript binded to the respective input? Aug 28, 2012 at 18:15
  • @AndreCalil No. I tried changing the name of the attribute in the model and view just in case there is some javascript bound that I don't know of, but still it did not work. Aug 28, 2012 at 18:59
  • Can you post your model and view code? Aug 28, 2012 at 19:03

2 Answers 2

5

I solved it. All that I needed was to call ModelState.Clear() prior to returning the new view.

1

Try initializing your string using String.Empty.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.