0

I'm using bootstrap 3.2.0, bootstrapvalidator 0.5.2, and mvc 5.1. I'm no expert in MVC, so the solution may be obvious to a more experienced user.

The problem I'm having is the model emits input element names in the format "modelname.fieldname", so, a username input element generated with

@Html.TextBoxFor(model => Model.LoginViewModel.UserName, new { @class = "form-control logininput", @placeholder = "User Name", @tabindex = 0 })

will generate html that looks like this:

<input class="form-control logininput" data-val="true" data-val-required="The Username field is required." id="LoginViewModel_UserName" name="LoginViewModel.UserName" placeholder="User Name" tabindex="0" type="text" value="">

The problem I'm having is that the bootstrap validator wants to reference the element by name, and if a period in the name (LoginViewModel.UserName) is invalid:

 $('#loginform').bootstrapValidator({
        fields: {
        LoginViewModel.UserName: {
                message: 'The user name is not valid',
                container: '#erruser',
                validators: {
                    notEmpty: {
                        message: 'The user name is required'
                    }
                }
            }
        }
    });

If I specify the name on the html helper, bootstrap validator works great, but the model won't validate:

@Html.TextBoxFor(model => Model.LoginViewModel.UserName, new { @class = "form-control logininput", @placeholder = "User Name", @tabindex = 0, Name="UserName" })

I've also tried escaping and bracketing, to no avail:

LoginViewModel\\.Password: {

and

LoginViewModel['Password']: {

Is there a way to make the bootstrapvalidator accept element names with a period, or do I have to modify the model in some way to map "simple" element names, to the model field name?

Sparky
  • 98,165
  • 25
  • 199
  • 285
ScottC
  • 51
  • 1
  • 4
  • What happens if you quote it? `fields: { 'LoginViewModel.UserName': { ..` –  Nov 04 '14 at 03:59
  • You have to override attribute name as "Name". Note that starts capital. http://stackoverflow.com/questions/6057865/asp-net-mvc-3-override-name-attribute-with-textboxfor#answer-17638187 – bashkan Nov 05 '14 at 21:21
  • Gentlemen, I found a solution. Because it is a strongly typed partial, messing around with the name is a bad idea. So instead, I used bootstrapValidator declaratively, which worked great! – ScottC Nov 07 '14 at 00:23

0 Answers0