1

Dazed and confused here... The field in question is a boolean, and I want the UI to be a checkbox (or a yes/no or on/off jQuery slider). Each time I try to add in this checkbox input, I end up getting a

Microsoft JScript runtime error: DOM Exception: HIERARCHY_REQUEST_ERR (3)

error.

Here's the HTML+Razor

 <div data-role="fieldcontain">
    <fieldset data-role="controlgroup">
       <legend>End Game:</legend>
       @Html.TextBoxFor(model => model.HandicapSession.WinByTwo, new { @type="checkbox" })
       <label for="WinByTwo">Win By Two?</label>
    </fieldset>
</div>

Here's the generated HTML:

<div data-role="fieldcontain">
    <fieldset data-role="controlgroup">
       <legend>End Game:</legend>
       <input data-val="true" data-val-required="The WinByTwo field is required." id="HandicapSession_WinByTwo" name="HandicapSession.WinByTwo" type="checkbox" value="False" />
       <label for="WinByTwo">Win By Two?</label>
    </fieldset>
</div>

Apparently this error occurs when there are conflicting or overlapping id's as jQuery Mobile creates its checkbox widget

$.widget( "mobile.checkboxradio", $.mobile.widget, { ...etc...

But how do I use the HTML and/or razor to make a simple checkbox work in jQuery Mobile?

tereško
  • 58,060
  • 25
  • 98
  • 150
AR.
  • 39,615
  • 9
  • 44
  • 52

2 Answers2

2

If the problem with jQuery Mobile really is duplicate names for the HTML tags, then you'll have to render your own input type=checkbox tag in HTML, as the ASP.NET MVC Html.CheckBoxFor helper method will render an input type=hidden tag with a duplicate name value. See this post for a discussion.

The hidden form tag is there because if you submit an unchecked checkbox to the server, the form value for that field isn't included. So a hidden input tag is included with value=false so that if the checkbox is unchecked, the value false is still submitted. The model binding process in MVC will filter out the duplicate form values for you, but if you're having a client-side problem with the duplicate name attributes, you'll have to render your own checkbox and label in HTML, then handle the fact that no value will be submitted for the HandicapSession.WinByTwo property when the box is unchecked. If no other property for HandicapSession is submitted, then that whole object will be null.

So, you can manually create the checkbox input and still load the checked and value attributes from your model, as desired, but you can run into model binding problems where the value for WinByTwo will still be false even when the box is checked.

Note also that the for attribute on your label doesn't match the ID of the checkbox in your sample. You need the full HandicapSession_WinByTwo.

The following manually creates the input tags:

<link rel="stylesheet"  href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />  
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>

<div data-role="page">
    <div data-role="content"> 
        <div data-role="fieldcontain">
            <fieldset data-role="controlgroup">
               <legend>End Game:</legend>
               <input type="checkbox" id="HandicapSession_WinByTwo" name="HandicapSession.WinByTwo" @(Model.HandicapSession.WinByTwo ? "checked=checked" : "") value="@(Model.HandicapSession.WinByTwo.ToString().ToLower())" />
               <label for="HandicapSession_WinByTwo">Win By Two?</label>
            </fieldset>
        </div>
    </div>
</div> 

The HTML output is as follows for a checked checkbox on load:

<div data-role="page">
    <div data-role="content"> 
        <div data-role="fieldcontain">
            <fieldset data-role="controlgroup">
               <legend>End Game:</legend>
               <input type="checkbox" id="HandicapSession_WinByTwo" name="HandicapSession.WinByTwo" checked=checked value="true" />
               <label for="HandicapSession_WinByTwo">Win By Two?</label>
            </fieldset>
        </div>
    </div>
</div>

The best would be just to use the MVC helper methods, so I'm not sure if you'd tried the following. The default Html.CheckBoxFor and Html.LabelFor helper methods work with jQuery Mobile 1.0a4.1 or 1.1.0 just fine. The following works for me:

<link rel="stylesheet"  href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />  
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>

<div data-role="page">
    <div data-role="content"> 
        @using (Html.BeginForm())
        { 
            <div data-role="fieldcontain">
                <fieldset data-role="controlgroup">
                   <legend>End Game:</legend>
                   @Html.CheckBoxFor(m => m.HandicapSession.WinByTwo)
                   @Html.LabelFor(m => m.HandicapSession.WinByTwo, "Win By Two?")

                   <input type="submit" id="SubmitForm" value="submit" />
                </fieldset>
            </div>
        }
    </div>
</div>

This produces the HTML output:

<div data-role="page">
    <div data-role="content"> 
        <form action="/Mobile/Mobile" method="post">            
             <div data-role="fieldcontain">
                <fieldset data-role="controlgroup">
                   <legend>End Game:</legend>

                   <input checked="checked" data-val="true" data-val-required="The WinByTwo field is required." id="HandicapSession_WinByTwo" name="HandicapSession.WinByTwo" type="checkbox" value="true" />
                   <input name="HandicapSession.WinByTwo" type="hidden" value="false" />
                   <label for="HandicapSession_WinByTwo">Win By Two?</label>

                   <input type="submit" id="SubmitForm" value="submit" />
                </fieldset>
            </div>
        </form>    
    </div>
</div> 
Community
  • 1
  • 1
nekno
  • 19,177
  • 5
  • 42
  • 47
0

Fix might be pretty simple.

Choice 1: Go scorched earth and turn off Ajax based navigation. This will ensure that unique IDs stay unique. This will ensure that you never encounter this issue again. Only downside is that you lose the pretty little transitions from page to page (someone call the whambulance). You can do this by setting up a global configuration script...

script src="jquery.js"
script src="custom-scripting.js"
script src="jquery-mobile.js"

inside that you'll override the ajax settings to disable them...

$(document).bind("mobileinit", function(){
    $.mobile.ajaxEnabled = false;
});

Choice 2: Call pages that are going to require this uniqueness with a link that has an attribute of rel='external' or target='_black' which will accomplish the same thing without disabling all ajax based navigation. If you are reaching the page as a results of a form post, you can use data-ajax='false' on the form tag to accomplish a clean load.

sgliser
  • 2,071
  • 12
  • 13
  • Thanks for this. I had already completely disabled Ajax-based navigation. jQuery Mobile's hash navigation just caused too many problems. The issue seems to be more that duplicate 'id's or 'name's are being generated and JQ gets confused. But damned if I can find where/why/how. – AR. Apr 13 '12 at 15:13
  • can you post the source somewhere? – sgliser Apr 13 '12 at 15:40