jQuery's .data() getter only gets elements that are set initially with a value
<p>... but if the value is originally empty like "" the value can not be changed using the same key !</p>

<pre><code>function loadDialog(link, e, ajaxRequest) {

    e.preventDefault();
    var $title = link.innerHTML;
    var $contenturl = $(link).attr('href');
    var $dialog = $('&lt;div id="MyDialog"&gt;&lt;/div&gt;');

    // Read value from hidden input field
    var templateIdValue = $('input[name=TemplateId]').val();     

    // Change the value: This will always and only happen the 2nd time I come here,
    // the first time the templateIdValue will always be empty
    if (templateIdValue != '') {
        templateIdValue = 777;
    }

    $dialog.load($contenturl).data('templateIdKey', templateIdValue).dialog({
        title: $title,
        autoOpen: true,
        modal: true,               
        buttons: {
            "OK": function () {debugger;
                ajaxRequest($(this), $('form', this));
            },
            "Cancel": function () {
                $dialog.dialog("close");
            }
        }
    });
}
</code></pre>

<p>When the 2nd time the templateIdValue is changed to 777</p>

<p>I come to this code:</p>

<p>Here I retrieve again the value and it should be 777. But its NOT. its empty !</p>

<pre><code>@model ITMS.Web.Models.UnitViewModel

@*Remote Validation*@
&lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;

&lt;script type="text/javascript"&gt;

    $(document).ready(function () {
        var templateId = $('#MyDialog').data('templateIdKey');
        $('input[name=TemplateId]').val(templateId);      
    }); 

&lt;/script&gt;

@using (Html.BeginForm("Create", "Unit"))
{ 
     @Html.ValidationSummary(false)
    &lt;p class="editor-label"&gt;@Html.LabelFor(model =&gt; model.Name)&lt;/p&gt;
    &lt;p class="editor-field"&gt;@Html.EditorFor(model =&gt; model.Name)&lt;/p&gt;
    &lt;p class="editor-field"&gt;@Html.ValidationMessageFor(model =&gt; model.Name)&lt;/p&gt; 

    @Html.HiddenFor(x =&gt; x.TemplateId)    
}
</code></pre>

<p>How can I change the value of the key I passed the first time with the .data() method?</p>

<p><strong>UPDATE</strong></p>

<pre><code>  // Does not cache the ajax requests to the controller e.g. IE7/8/9 is doing that...
    $.ajaxSetup({ cache: false });

    $(document).ready(function () {
        $('#OpenTemplate').click(function (event) { loadDialog(this, event, openTemplate); });
        $('#CreateTemplate').click(function (event) { loadDialog(this, event, createTemplate); });
        $('#DeleteTemplate').click(function (event) { loadDialog(this, event, deleteTemplate); });

        $('#CreateUnit').click(function (event) { loadDialog(this, event, createUnit); });
        $('#DeleteUnit').click(function (event) { deleteUnit(); });

        $('#CreateTeststep').click(function (event) { loadDialog(this, event, createTeststep); });
        $('#DeleteTeststep').click(function (event) { deleteTeststep(); });

        $('#SaveTemplate').click(function (event) { saveTemplate(); });    
    });
</code></pre>
