Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD

I was getting a JavaScript error that pointed to line number 1 of my MVC page. I have a form on the page and expected the Unobtrusive Validation to work with the Data Annotations. It took me a while to realize the validation messages weren't showing. After I while I found that I was missing the @Html.ValidationMessageFor(m => m.Name). Adding that for each field fixed it.

My password reset with token example. I was missing line 12 and 17.

   1:  @using (Html.BeginForm("PasswordReset", "Home"))
   2:  {
   3:      @Html.AntiForgeryToken()
   4:      @Html.ValidationSummary()
   5:   
   6:      <fieldset>
   7:          <legend>@HomeViewResource.ChangePasswordFormTitle</legend>
   8:          <ol>
   9:              <li>
  10:                  @Html.LabelFor(m => m.PasswordResetModel.NewPassword)
  11:                  @Html.PasswordFor(m => m.PasswordResetModel.NewPassword)
  12:                  @Html.ValidationMessageFor(m => m.PasswordResetModel.NewPassword)
  13:              </li>
  14:              <li>
  15:                  @Html.LabelFor(m => m.PasswordResetModel.ConfirmPassword)
  16:                  @Html.PasswordFor(m => m.PasswordResetModel.ConfirmPassword)
  17:                  @Html.ValidationMessageFor(m => m.PasswordResetModel.ConfirmPassword)
  18:              </li>
  19:          </ol>
  20:          <input type="submit" class="btn btn-primary" value="@HomeViewResource.ChangePasswordButtonText" />
  21:      </fieldset>
  22:   
  23:      @Html.HiddenFor(m => m.PasswordResetToken)
  24:      @Html.HiddenFor(m => m.UserId)
  25:  }
Posted on Monday, May 20, 2013 1:45 PM MVC , JavaScript | Back to top


Comments on this post: Uncaught SyntaxError: Unexpected token u. file JavaScript Error in MVC

# re: Uncaught SyntaxError: Unexpected token u. file JavaScript Error in MVC
Requesting Gravatar...
Thanks. I was having the exact same issue and this solved my problem.
However, in my case it was missing the ValidationMessageFor in the rememberme checkbox (which I didn't want). The form was getting posted to the server and I was getting the 'unexpected token u' in the browser console.
Solutions,
- add ValidationMessageFor rememberme or,
- Make the bool RememberMe nullable on your LoginModel or,
- Add DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; on Application_Start() of global.asax

Hope this helps someone.
Left by ad_srv on Jan 22, 2014 11:58 AM

# re: Uncaught SyntaxError: Unexpected token u. file JavaScript Error in MVC
Requesting Gravatar...
Thanks it helped for me.
Left by Vanaja on Oct 09, 2015 4:42 AM

# re: Uncaught SyntaxError: Unexpected token u. file JavaScript Error in MVC
Requesting Gravatar...
Thank you sir very much, this solved my problem after a lot of useless debugging. Thanks again sir world with people like you is much better place :))
Left by Ibrahim Amer on Jan 05, 2016 9:33 AM

# re: Uncaught SyntaxError: Unexpected token u. file JavaScript Error in MVC
Requesting Gravatar...
Use ValidationMessageFor for applicable properties, dnt miss any
Left by Soumen on Dec 08, 2016 3:29 AM

Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net