Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC crazy property lose its value. Does Html.HiddenFor bug?

Tags:

c#

asp.net-mvc

There something really weird happening in my MVC application that drives me crazy. On my MVC Page, after a user got selected, it's Login should be "rendered" twice on the HTML. Once in the form of

FrmNextStep('<Login>', ...
(where Model.SelectedUser.Login is used)

and once in the form of

<input id="SelectedLogin" name="SelectedLogin" value="<Login>" type="hidden">
(where "Model".SelectedLogin is used)

but the second one always stay blank. It's really weird because, despite the two call not being exactly the same, the return value should be.

if (Model.SelectedUser != null)
{
    <span>Some value</span>
    <script type="scriptADResultComplete">
    @{
        var script = String.Format(
            @"FrmNextStep('{0}', '{1}', '{2}');"
            , Model.SelectedUser.Login.Replace("'", @"\'")
            , Model.SelectedUser.FirstName.Replace("'", @"\'")
            , Model.SelectedUser.LastName.Replace("'", @"\'")
        );

        @Html.Raw(script);
    }
    </script>
}

<input type="hidden" name="hfAction" />
<input type="hidden" name="hfUserLogin" />
@Html.HiddenFor(m => m.CurrentPage, new { id = "hfCurrentPage" })
@Html.HiddenFor(m => m.SelectedLogin);


    private User selectedUser;
    public User SelectedUser
    {
        get
        {
            if (this.selectedUser == null)
            {
                this.selectedUser = this.AllUsers.FirstOrDefault(user => user.Selected) ?? User.DefaultUser;
            }

            if (this.selectedUser == User.DefaultUser)
            {
                return null;
            }

            return this.selectedUser;
        }
        set
        {
            this.AllUsers.ForEach(user => user.Selected = (user == value));

            this.selectedUser = null;
        }
    }

    public string SelectedLogin
    {
        get
        {
            return (this.SelectedUser ?? User.DefaultUser).Login;
        }
        set
        {
            this.SelectedUser = this.AllUsers.FirstOrDefault(user => user.Login == value);
        }
    }

And when I have debug the code,the only call to Selected Login during the "rendering" phase and return the correct login.

Is there any bug with Html.HiddenFor?

like image 468
Serge Avatar asked May 29 '13 14:05

Serge


1 Answers

Ok, it's actually a bug/faulty behaviour of Html.HiddenFor.
The idea of this possibility only came to my mind while I was writing my question.


I changed it into:

<input type="hidden" value="@Html.AttributeEncode(Model.SelectedLogin)" id="SelectedLogin" name="SelectedLogin" />

and it's working perfectly fine.

EDIT:

There's another workarround.

Call ModelState.Clear(); in the controler post action.
I'll use this option.

ModelState.Clear();

+

@Html.HiddenFor(m => m.SelectedLogin)
like image 182
Serge Avatar answered Sep 28 '22 08:09

Serge