SoftwareSavant
SoftwareSavant

Reputation: 9775

Hidden fields without a form in MVC

I have a table and no form in one page that I am working with. Is there any way to persist certain values to that html without creating a form. I will not be submitting from this page in any specific way.

<!DOCTYPE html>
<html>
<head>
    <title>Show All Encounters</title>
</head>
<body>
<div class="content-wrapper clear-fix float-left"  style="height: 1px; padding: 10px;" id="list1">
    @{        
        Html.Hidden("popId", TempData["POPULATIONID"], new { id = "hidPopID" });
        Html.Hidden("popId", TempData["POPNAME"], new { id = "hidpnID" });
        Html.Hidden("popId", TempData["ROWNUM"], new { id = "hidrownumID" });
    }
 <table border="2" id="frTable" class="scroll"></table>  
 <div id='pager'></div>
 </div>
</body>
</html>
<script>
    function loadDialog(tag, event, target) {
        //debugger;
        .load($url)
      .dialog({
      ...
      , close: function (event, ui) {
            debugger;
            //if($url.contains)        
            var popId = $('#hidPopID').val();
            var rows = $('#hidrownumID').val();
            location.reload(true);
      }
        });

        $dialog.dialog('open');
    };
    </script>

that close event is inside of a jquery Modal dialog call btw, so the syntax is technically correct

Upvotes: 1

Views: 1739

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039578

You could persist the values wherever you want in the DOM but if you want to send them to the server you have a couple of options:

  • Form with hidden fields (you said you don't want this)
  • AJAX request that will harvest the values from the DOM
  • Anchor with the values in the query string

Or simply persist the values on the server. I guess that you are using some data store there which could be used.


UPDATE:

Now that you have shown your code it is clear why it is not working. You do not have any hidden fields (browse at the HTML source code in your browser to see that they are missing). You have just called the Html.Hidden helper on the server but you never outputted the result to the HTML

Now add your hidden fields correctly:

@Html.Hidden("popId", TempData["POPULATIONID"], new { id = "hidPopID" })
@Html.Hidden("popId", TempData["POPNAME"], new { id = "hidpnID" })
@Html.Hidden("popId", TempData["ROWNUM"], new { id = "hidrownumID" })

Upvotes: 2

Related Questions