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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With