1

I want to submit a form, but the button isn't clickable. After some search I found out that this is due to the jquery validation that ASP.NET MVC has for required fields. For some weird reason ASP NET thinks my hidden field "UserId" is required. Which isn't actually true. See part of my model here:

   public class ResetPasswordModel
   {

        [.....]

     public Guid UserId
     {
       get;
       set;
     }

   }

And the page source shows this:

<input data-val="true" data-val-required="The UserId field is required." id="UserId" name="UserId" type="hidden" value="" />

Any ideas?!?

1
  • Could you post the UserId property code with it's attributes. Commented Jan 12, 2012 at 15:33

5 Answers 5

1

The fact it's not a nullable type means it's required. Change the definition to Guid? (include the question mark to make it nullable) Or even better create a viewmodel that doesn't have it.

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

Comments

1

I believe that a Guid is required by default because it is not nullable. Guids are usually primary keys so making them nullable is problematic. Try converting your guid ToString() so that you can use it in your view, then back to a Guid before using it in your controller or model. You may have to create a ViewModel in order to do this.

Comments

0

Are you sure there isn't a separate partial class containing MetaData which has marked the field as mandatory?

Something like this:

class UserResetPasswordModelMetaData
{
    [Required(ErrorMessage = "The UserId field is required.")]
    public Guid UserId { get; set; }
}

Search your project for the string "The UserId field is required". I bet it's there somewhere.

Also, how do you identify the user who is trying to reset their password, if not by their id?

4 Comments

I'm not sure what you mean by the seperate class. This is the page that was emailed to the specific user so I already have his id and I just want to pass it to my post action, that's why I have this hidden field.
Yeah, based on the code that was provided in the question it doesnt appear to me that the UserID field would be required. There is no meta markup for it. Are you using Html.HiddeFor in your view? Have you set the value of UserID in your view model before rendering it out to the page?
Dear God. I'm so sorry for my post. I just saw that I wasn't rendering my model to the view... ah, I can't believe it!!!!!!! ...
+1 for honesty. Lol! I did wonder! I appreciate your candour. I guess mine is the right answer then... ahem ;-)
0

setup the jQuery validator to ignore hidden fields. Like so:

jQuery Validate Ignore elements with style

Comments

0

Probably There is a Key attribute on top of UserId property which means it's mandatory

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.