6

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?

0

2 Answers 2

14

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)
1
  • 5
    Not the downvoter, but the reason might be that this isn't a "bug/faulty behaviour of Html.HiddenFor". If using ModelState.Clear() works, it seems likely (can't tell from your code) that there's a SelectedLogin property in your ModelState (e.g. a value POSTed to the controller action), which is different from the value on your model. The HTML helpers will always choose ModelState over Model when picking the value. This is by design, not a bug.
    – JimmiTh
    Jul 29, 2013 at 23:58
0

I had this issue also, with the .cshtml executing twice while returning the page, and the second run having issues with null model objects.

Using @Serge's answer fixes the HiddenFor's, but then I also had issues with partials that can't be solved this way.

It turns out to be an intermittent issue, where the Controller method that invokes the view is setting ViewBag properties together with returning the view+model separately via protected internal ViewResult View(string viewName, object model);.

The project I'm working on has this all over the place, and on other controllers/views it works fine. But for the one I was maintaining, this null issue in the HiddenFor's was happening.

Anyway, I pulled all the ViewBag setters out of the controller, and moved them into NotMapped properties on the model object.

Once this was done the HiddenFor calls etc all started behaving again. So, don't mix your ViewBag and ViewModel kids.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.