James123
James123

Reputation: 11652

Keep hiddenField value on postback in MVC

I have hiddenfield in _layout file. When user logged in I am checking a value in database. if it is true poping up a dialog box to user. if user close the dialog box I will not show the message box he loggout. so I am doing like this.

_layout

@Html.Hidden("BadAddressWarning","")

layout.js

    $(function () {
        if ($("#BadAddressWarning").val() == "") {
        $.ajax({
            url: '../Address/CheckPrimaryAddressGood',
            type: "Get",
            ....
           success: function(data) {
            if (data != "") {                
            $("#dialogCheckAddress").dialog({
                  ...
            });
            $("#dialogCheckAddress").dialog("open");
            $("#dialogCheckAddress").on("dialogbeforeclose", function(event, ui) {
                   $("#BadAddressWarning").val("false");
            });
            }

            },
            failure: function(errMsg) {
                alert(errMsg);
            }
        });
    }
});

If I do post back. I am losing $("#BadAddressWarning").val(). I would like retain that value.

Upvotes: 0

Views: 1580

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239240

You're explicitly passing an empty string as the value. Instead, do something like:

@Html.Hidden("BadAddressWarning", Request["BadAddressWarning"])

Or even simpler:

@Html.Hidden("BadAddressWarning")

Upvotes: 1

Related Questions