0

been trying to fix this for a few days now and just cant get it to work!

i have a radio button list which determines the output of a form - one of the radio buttons is to download X amount of files. this radio button has a text box for the user to enter the amount (X) they wish to download.

i only need this textbox to validate if the radio button that corresponds to it is selected - this is what i have so far but cannot get it to work. any help would be appriciated.

MODEL

public class myClass

{
       [Required(ErrorMessage = "Please select the type of output you wish to generate")]
       public int providerType { set; get; }


       public int? numOutput { set; get; }


       public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
       {
           if (providerType == 2 && numOutput == null)
               yield return new ValidationResult("Description must be supplied.");
       }

}

CONTROLLER

    [AcceptVerbs(HttpVerbs.Post)]
    [HttpPost, ValidateInput(false)]
    public ActionResult Spin(myClass theData)

    {

        int? ProviderType = Convert.ToInt16(Request.Form["providerType"]);

        if (ModelState.IsValid)
        {

            //other random processing

        }

    }

VIEW

   <ul>
<li>
  <%=Html.RadioButton("ProviderType","1")%><label>Output A Single Article (With Visible HTML Tags)</label>
 </li>
 <li>
  <%=Html.RadioButton("ProviderType","4")%><label>Output A Single Article (With HTML Pre Rendered - Not Recommended For Articles With Videos)</label>
 </li>
  <li>
  <%=Html.RadioButton("ProviderType", "3")%><label>Output For Mass Submission</label> 
  </li>

 <li>
  <%=Html.RadioButton("ProviderType", "2")%><label>Download Several Copies Of Article (With Visible HTML Tags)</label>
 How Many Artilces (Max 20) 

            <%= Html.TextBoxFor(Model => Model.numOutput)%>
             <%= Html.ValidationMessageFor(Model => Model.numOutput)%>

im still new to MVC so im probably doing something stupid here - any help is much appriciated. The error im getting is that when i select the radio button and dont enter anything in the textbox "Input string was not in a correct format." so obviously the validation is not firing - cant quite figure out why.

4
  • How are you trying to validate it? with model.validations or with js? if it is in the model then you have validate input turned off so it won't ever catch it. Commented Jun 8, 2012 at 17:37
  • Thats probably why - i disabled it because there are textboxes on the page that im allowing html tags to be input in. Although there is other [required] validation in the model that is being executed correctly Commented Jun 8, 2012 at 17:45
  • Ok i removed validate input = off to test it and still had no luck. Commented Jun 8, 2012 at 17:48
  • @Brian - ValidateInput does not control model validation, it controls input validation, which ensures that the input does not contain dangerous code. It has nothing to do with model validation. Commented May 19, 2013 at 10:00

1 Answer 1

2

The reason why its not hitting Validation is because your model is not implementing IValidatable Object.

  e.g. public class MyClass:IValidatableObject

Let me know if that works for you. I was able to get the above code working locally and can send you the code if it still doesnt work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

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.